Thursday, November 23, 2017

Drupal 8: Add custom classes to pages using a field value

I have many projects where one special page needs a totally different design than the other pages. So in the css you need a different setup, for instance no right bar with a menu.
I always write the pathname and the node-id to the body classes, but oftentimes you need to change the path of the pages, or you want to add other similarily designed pages to the set and then you need to add those path-classes to the css also, and this all apart from the fact that paths and node ids sometimes diverge in the development environment and the production environment.

The class is strong with this one
So I made a very simple theme preprocess function which simply writes the field value of a custom
field to the bodyclass. This way you can add pages to this special design by simply writing the same class value in that field.
I added a text field 'field_bodyclass' tot the contentypes, and hidden it in the visibility settings for these contentypes. And using this preproces fs function:

function MyModuleOrMyTheme_preprocess_html(&$variables) {
  $node = \Drupal::routeMatch()->getParameter('node');
  if($node) {
     $variables['attributes']['class'][] = $node->field_bodyclass->value;
  }
 .... other code.....
}

Sunday, April 10, 2016

Drupal 8 - Unable to install Devel, system.menu.devel already exists in active configuration.

To protect and to serve....
Well life is shit sometimes.
I migrated a Drupal 6 site to Drupal 8.
All I wanted is to install devel and then develop like mad.
But  Devel won't install. It says:
Unable to install Devel, system.menu.devel already exists in active configuration.
Diving into Drupal 8 I solved it, but I must honestly tell you that I do not really understand all consequences. It has something to do with the dependencies. The old site had Devel running, so maybe that caused the problem....


Solution (providing you have drush installed)
Open terminal and navigate to the root folder of your website
$ drush php
Wait till the drush php prompt (>>>) appears
type
Drupal
::configFactory()->getEditable('system.menu.devel')->delete();
push enter
some message appears like => Drupal\Core\Config\Config {#8157}
type
exit

After that I had no problem installing Devel (and develop like hell....)



Wednesday, January 7, 2015

Drupal 7 - changing overall site language

So, as I am used to read and install everything in english, half of the sites we build are actually only in Dutch. That is, overall site language including the admin interface. I rather like to have the admin interface in english, cause you can more easily google the web on any problems using the english system messages.
So then: All the time I start drushing a site install, enabling my module sets, running some features, focusing on the technical design and the custom functionalities we have to build,  and then suddenly realize that the whole lot has to be in Dutch. Or German. Or Latin, or French or Klingon.

Let's all Klingon

So, how did we change the site language again?

1. Enable the Locale module
2. Download Localization update
3. Enable Localization update
(didn't expect that eh?)
4. Go to Configuration > Languages
(/admin/config/regional/language)
5. Add the language of your choice
(The module will then import the Drupal translation files for the core and all installed modules)
6. Change the default language and Voila, as the French say, your site language has changed.









Monday, October 27, 2014

Drupal - Rebuild permissions

Because I always forget where to find the rebuild permissions button in Drupal, this is my reminder


drupal 6   /admin/content/node-settings/rebuild
drupal 7   /admin/reports/status/rebuild


I actually do not have permission to use this image....

Thursday, October 16, 2014

Drupal 7 - apache solr - how to add an total results count to your results

Somehow I naturally expected a total results entry somewhere in the arrays solr sends me, but I could not find it.
So, what is so weird about wanting to show the number of results?

The solution is of course rather simple, like everything is simple in hindsight, but nevertheless it took me hours to come up with it. Mostly because I was googling through endless hits more or less about this problem and maybe I should try to catch some more sleep....

The solution is to lift the value from the GLOBALS array.  Add  $GLOBALS['pager_total_items'][0] somewhere to a variable in a preprocess hook or simply print in in your template file like thus:


<div class="counter"><?php print $GLOBALS['pager_total_items'][0].' results'; ?></div>



Thursday, September 18, 2014

Drupal 7 - Picture - Adding captions to responsive images

I've setup an D7 site (a D6 upgrade) with Picture and Breakpoint where I want to show a caption for some of the images. In the  D6 version used the title attributes as captions for some of the images with a phptemplate_imagecache function. (Depending on the image style used.)

Oh caption, my caption
There are some modules (https://www.drupal.org/project/image_caption, https://www.drupal.org/project/image_field_caption, https://www.drupal.org/project/caption_filter, etc) which give you the possibility of adding a caption field, but as I do not want to build another module to convert all existing title tags to these caption attributes, and also need a solution for non javascript browsers, I needed something more basic.

Specifications
Based on the specific imagestyle or specific picture mapping, decide to render or not render a caption. The content of the caption is the content of the title attribute.

Solution
Add a theme function to your theme's template.php
The idea is to simply override the theming function in picture (which is the module I use for my responsive images)


// this theming function assumes that the picture module is installed
function THEMENAME_picture($variables) {
  // first produce the  standard picture output
  $output=theme_picture($variables);
  // we only add titles to the images where a specific picture mapping is defined
  if (array_key_exists('breakpoints', $variables) && array_key_exists('title', $variables)){
     if ($variables[breakpoints]['custom.user.normal']['1x'][image_style] =='rsp_adviesplaatjecustom_user_normal_1x'){
        if ($variables['title'] !=''){
           $output=$output.'<div class="image-caption"><div class="image-caption-text">'.$variables['title'].'</div></div>';
        }
     }
  }
  return $output;
}






Wednesday, August 20, 2014

Drupal - Omega 4.x - layouts - how to create a layout and add a sidebar (or anything)

Lets start with a giant disclaimer - my lawyer advised me thus, to always start with a disclaimer, even if I am just entering a supermarket or visiting a friend for a coffee:
I don't know if all here under is the correct way to proceed.
Adding a layout as shown in the leveltuts tutorials did not work. But I  probably just forgot a step, multiple times, or multiple steps at different times in the process. Anyway, I decided to do some low level cortex manipulation and lo and behold, that worked.
Low level cortex being simple copy-paste and renaming magic.

I wanted a layout with an extra region in it, and it seems that the best way to do that is to add a layout to your omega subtheme. As I am still novicing into the omega 4.x sass setup, I could be totally wrong on this. 


Well, anyhow, here my cookbook:
1. Create  (if not already there) a folder 'layouts' in your omega subtheme folder
(So in my case the subtheme is called rli2015 and the layouts folder in /sites/all/themes/rli2015/layouts)
2. copy the folder simple from your omega base theme to the layouts folder you just created (sites/all/themes/omega/omega/layouts/simple)
3. Rename this folder to what you wanna call your layout.
I called it allpages as I wanted to create a layout I can use over the whole site
3.1 rename the folder to your layout name. In this aexample allpages.
3.2 rename the file simple.pages.inc to allpages.layout.inc.
3.3 rename the file simple-layout.tpl.php to allpages-layout.tpl.php.
3.4 In the allpages.layout.inc change:
3.4.1 name=allpages
3.4.2 description = whatever text you want here
3.4.3 preview= allpages.png (if you want to make an image for this layout, you can also leave it out)
3.4.4 template=allpages-layout
(has to be what you named your tpl file under 3.3)
3.4.5 Stylesheets (you can leave this one out, or simply add an empty one for later use)

; Stylesheets 
stylesheets[all][] = css/layouts/allpages/allpages.layout.css
3.4.6 Add a region under the regions section

; Regions
regions[branding]       = Branding
regions[header]         = Header
regions[navigation]     = Navigation bar
regions[highlighted]    = Highlighted
regions[help]           = Help
regions[content]        = Content
regions[sidebar_first]  = First sidebar
regions[sidebar_second] = Second sidebar
regions[sidebar_third] = Third sidebar
regions[footer]         = Footer
3.5 Also add this region to your inc file under your subtheme itself, in my case rli2015.inc
regions[sidebar_third] = Third sidebar
3.6 In your tpl file (allpages-layout.tpl.php) add the region you just added where you want it rendered

<?php print render($page['sidebar_third']); ?>
NB! If there is no content in the region, the region will not be rendered
3.7 Copy the omega/sass/layouts/simple folder to your custom theme sass layouts folder
3.7.1 Rename simple to allpages
3.7.2  Rename simple.layout.scss to allpages.layout.scss (has to be the same as the one you declared under 3.4.5)
3.7.3 Strip it of all its content.
4. Go to admin/config/development/performance.
5. Clear all caches.
6. Go to appearance.
7. Choose the settings of your subtheme.
(in my case admin/appearance/settings/rli2015)
8. Enable layout extensions.
9. Select your custom layout and save the configuration.

After that you have a html output with your extra third sidebar (providing you've added content to that sidebar....)

If you have guard (bundle exec guard) running on your subtheme directory the css file should be automatically created under /sites/all/themes/rli2015/css/layouts/allpages.
If not, add the allpages.layout.css file by hand.

NB! GUARD!
I had the problem that guard (bundle exec guard)
stopped working. It simply did not compile.
Dunno why...
Instead of that I ran compass watch multiple times in the same directory, stopping it with ctrl-c. And after some iterations of that I got messages about changes made. And after that, bundle exec guard also ran again. Dunno why....


.