Planet Drupal

Subscribe to Planet Drupal feed
Drupal.org - aggregated feeds in category Planet Drupal
Updated: 14 min 32 sec ago

Dries Buytaert: State of Drupal presentation (May 2016)

Thu, 12/05/2016 - 20:25

DrupalCon New Orleans comes at an important time in the history of Drupal. Now that Drupal 8 has launched, we have a lot of work to do to accelerate Drupal 8's adoption as well as plan what is next.

In my keynote presentation, I shared my thoughts on where we should focus our efforts in order for Drupal to continue its path to become the leading platform for assembling the world's best digital experiences.

Based on recent survey data, I proposed key initiatives for Drupal, as well as shared my vision for building cross-channel customer experiences that span various devices, including conversational technologies like Amazon Echo.

You can watch a recording of my keynote (starting at 3:43) or download a copy of my slides (162 MB).

Take a look, and as always feel free to leave your opinions in the comments!

Categories: Elsewhere

ThinkDrop Consulting: Onward with OpenDevShop Inc

Thu, 12/05/2016 - 18:27

Today I am awaking to the last "official" day of DrupalCon New Orleans with a huge new wind at my back.

It felt like an appropriate time to post what is likely my last blog post as ThinkDrop Consulting LLC.

My partners and I have been in a whirlwind tour of the convention, spreading the news of our product, and our new company: OpenDevShop Inc. In order to focus entirely on development and hosting tools, I am closing up ThinkDrop Consulting.

We've been building the OpenDevShop platform since late 2011 for my clients and myself, and in January of this year, we incorporated.

Our mission: to make hosting, testing, and scaling websites as easy as possible; to make infrastructure management as easy to deal with as content; and to foster a community around these types of tools.

OpenDevShop Inc is a member of the newly formed Aegir Coop. The cooperative is a group of companies and individuals who have organized together to support and develop the Aegir ecosystem.

We are working hard to not only grow this business but to grow the Aegir community, both users but especially contributors. We have a lot of work to do if Aegir is going to live up to modern expectations of infrastructure management and deployment tools.

Today we have two Birds of a Feather sessions back to back at DrupalCon: one for OpenDevShop and one for the Aegir Coop.

The energy behind the Drupal Community has never been higher. Let's follow in their footsteps and bring together everyone that cares about better Infrastructure Management and DevOps.

Come join the Aegir & OpenDevShop communities in room 292 (AshDay) from 1pm - 3pm, and maybe later if they let us stay.

Please check out our new company website, opendevshop.com.

We will see you around the community!

Tags: devshopPlanet Drupal
Categories: Elsewhere

LevelTen Interactive: Learn with LevelTen: DrupalCon Session Twitter Recap

Thu, 12/05/2016 - 16:50

The LevelTen team used the hashtag #learnwithl10 to document the various sessions they attended and what they learned on Tuesday and Wednesday of DrupalCon New Orleans.

...Read more
Categories: Elsewhere

DrupalEasy: DrupalEasy Podcast: New Orleans Day 1

Thu, 12/05/2016 - 16:50

Direct .mp3 file download.

Hosts Ryan Price, Mike Anello, Ted Bowman and Kelley Curry are joined by guests Mike Herchel (of the Lullabot Podcast) and Steve Edwards (formerly of the Acquia Podcast) to discuss the events on Day 1 of DrupalCon. We start with the Prenote and Driesnote, and then discuss sessions each person was interested in throughout the day.

Check in later this week for more episodes from DrupalCon New Orleans 2016.

Follow us on Twitter Intro Music

House of Drupalcon from #Prenote

By Adam Juran, Campbell Vertessi and Jeremy "JAM" Macguire

Subscribe

Subscribe to our podcast on iTunesGoogle Play or Miro. Listen to our podcast onStitcher.

If you'd like to leave us a voicemail, call 321-396-2340. Please keep in mind that we might play your voicemail during one of our future podcasts. Feel free to call in with suggestions, rants, questions, or corrections. If you'd rather just send us an email, please use our contact page.

Categories: Elsewhere

OSTraining: Drupal 8 CookieConsent EU Law module

Thu, 12/05/2016 - 13:53

One of are OSTraining members asked how to add cookie notification to a drupal 8 site.

The CookieConsent is a module provides a solution to deal with the EU Cookie Law.

And is particularly useful if you want to use the SuperCookie module

Categories: Elsewhere

Wunderkraut blog: Dropcat - the configuration files

Thu, 12/05/2016 - 13:26

In a series of blog posts I am going to present our new tool for doing drupal deploys. It is developed internally in the ops-team in Wunderkraut Sweden , and we did that because of when we started doing Drupal 8 deploys we tried to rethink how we mostly have done Drupal deploys before, because we had some issues what we already had. This is part 2.

The idea with dropcat is that you use it with options, or/and with configuration files. I would recommend to use it with config files, and with minor settings as options. 

You could use just use a default settings file, that should be dropcat.yml, or as in most cases you have one config file for each environment you have – dev, stage, prod etc.

You could use an environment variable to set which environment to use, this variable is called DROPCAT_ENV.  To use prod environment you could set that variable in the terminal to prod with:
export DROPCAT_ENV=prod

Normally we set this environment variable in our jenkins build, but you could also set it as an parameter with dropcat like:
dropcat backup --env=prod

That will use the dropcat.prod.yml file

By default dropcat uses dropcat.yml if youi don't set an environment. 

Thing will be more in the next blog posts, but first we now look into a minimal config file, in our root dir we could hav a dropcat.yml file with this config:

app_name: mysite local: environment: tmp_path: /tmp seperator: _ drush_folder: /home/myuser/.drush remote: environment: server: mytarget.server.com ssh_user: myuser ssh_port: 22 identity_file: /home/myuser/.ssh/id_rsa web_root: /var/www/webroot temp_folder: /tmp alias: mysite_latest_stage site: environment: drush_alias: mysitestage backup_path: /backup original_path: /srv/www/shared/mysite_stage/files symlink: /srv/www/mysite_latest_stage/web/sites/default/files url: http://mysite.com name: mysitestage mysql: environment: host: mymysql.host.com database: my_db user: my_db_user password: my_db_password port: 3306

The settings is grouped in a way that should explain what they are used for – local.environment is from where we deploy, remote.environment is to where we deploy. site.environment is for drush and symlinks (we use for the files folder), mysql.environment, is for… yeah you guessed correctly – mysql/mariadb. 

appname

This is the application name, used for creating a tar-file with that name (with some more information, like build date and build number).

local

These are the settings from where we deploy, it could be localy, it could be a build server as jenkins. 

tmp_path

Where we temporary store stuff.

Seperator

Used for i name of foler to deploy as seperator like myapp_DATE


drush_folder

Where drush-settings from you deploy from, normaly in your home folder (for jenkins normaly: /var/lib/jenkins/.drush), and this is also to which path the drush alias is saved on dropcat prepare.

Remoteserver

The server you deploy you code too.

ssh_user

User to use with ssh to your remote server

ssh_port

Port used to use ssh to your server

identity_file

Which private ssh-key to use to login to your remote server

web_root

Path to which your site is going to be deployed to.

temp_folder

Temp folder on remote server, used for unpacking tar file.

alias

Symlink alias for you site


Sitedrush_alias

Name of you drush alias, used from 'local' server. Drush alias is created as a part of dropcat prepare.

backup_path

Backup path on ”local” server. Used by dropcat backup

original_path

Existing path to point a symlink to – we use for the files folder

symlink

Symlink path that points to original_path

url

URL for you site, used in drush alias

name

Name of site in drush alias.


Mysqlhost

name of db host

database

Database to use

user

Database user

password

password for db user to host

port

Port to use with mysql

We are still on a very abstract level, next time we will go through that is needed in an normal jenkins-build.

Categories: Elsewhere

Wunderkraut blog: Dropcat - the configuration files

Thu, 12/05/2016 - 13:26

In a series of blog posts I am going to present our new tool for doing drupal deploys. It is developed internally in the ops-team in Wunderkraut Sweden , and we did that because of when we started doing Drupal 8 deploys we tried to rethink how we mostly have done Drupal deploys before, because we had some issues what we already had. This is part 2.

The idea with dropcat is that you use it with options, or/and with configuration files. I would recommend to use it with config files, and with minor settings as options. 

You could use just use a default settings file, that should be dropcat.yml, or as in most cases you have one config file for each environment you have – dev, stage, prod etc.

You could use an environment variable to set which environment to use, this variable is called DROPCAT_ENV.  To use prod environment you could set that variable in the terminal to prod with:
export DROPCAT_ENV=prod

Normally we set this environment variable in our jenkins build, but you could also set it as an parameter with dropcat like:
dropcat backup –env=prod

That will use the dropcat.prod.yml file

By default dropcat uses dropcat.yml if youi don't set an environment. 

Thing will be more in the next blog posts, but first we now look into a minimal config file, in our root dir we could hav a dropcat.yml file with this config:

app_name: mysite local: environment: tmp_path: /tmp seperator: _ drush_folder: /home/myuser/.drush remote: environment: server: mytarget.server.com ssh_user: myuser ssh_port: 22 identity_file: /home/myuser/.ssh/id_rsa web_root: /var/www/webroot temp_folder: /tmp alias: mysite_latest_stage site: environment: drush_alias: mysitestage backup_path: /backup original_path: /srv/www/shared/mysite_stage/files symlink: /srv/www/mysite_latest_stage/web/sites/default/files url: http://mysite.com name: mysitestage mysql: environment: host: mymysql.host.com database: my_db user: my_db_user password: my_db_password port: 3306

The settings is grouped in a way that should explain what they are used for – local.environment is from where we deploy, remote.environment is to where we deploy. site.environment is for drush and symlinks (we use for the files folder), mysql.environment, is for… yeah you guessed correctly – mysql/mariadb. 

appname

This is the application name, used for creating a tar-file with that name (with some more information, like build date and build number).

local

These are the settings from where we deploy, it could be localy, it could be a build server as jenkins. 

tmp_path

Where we temporary store stuff.

Seperator

Used for i name of foler to deploy as seperator like myapp_DATE


drush_folder

Where drush-settings from you deploy from, normaly in your home folder (for jenkins normaly: /var/lib/jenkins/.drush), and this is also to which path the drush alias is saved on dropcat prepare.

Remoteserver

The server you deploy you code too.

ssh_user

User to use with ssh to your remote server

ssh_port

Port used to use ssh to your server

identity_file

Which private ssh-key to use to login to your remote server

web_root

Path to which your site is going to be deployed to.

temp_folder

Temp folder on remote server, used for unpacking tar file.

alias

Symlink alias for you site


Sitedrush_alias

Name of you drush alias, used from 'local' server. Drush alias is created as a part of dropcat prepare.

backup_path

Backup path on ”local” server. Used by dropcat backup

original_path

Existing path to point a symlink to – we use for the files folder

symlink

Symlink path that points to original_path

url

URL for you site, used in drush alias

name

Name of site in drush alias.


Mysqlhost

name of db host

database

Database to use

user

Database user

password

password for db user to host

port

Port to use with mysql

We are still on a very abstract level, next time we will go through that is needed in an normal jenkins-build.

Categories: Elsewhere

d7One: How to print orders in Commerce

Wed, 11/05/2016 - 18:53

In this tutorial or guide, I will share the best solutions I found for two basic Drupal Commerce use-cases and delve into their respective setup.

Commerce Kickstart 2 (CK2) is a great distribution for setting up an online store; it packs a lot of goodies out-of-the-box. But it can't have them all. Printing an order to PDF is not included. So one has to do some R&D for that.

Categories: Elsewhere

Valuebound: Your First Step to Git

Wed, 11/05/2016 - 14:39

Hey! So you are here in this page trying to find/learn something about git! Have you used a source code management system to synchronize your local code remotely before? Do you know that Git is the most powerful SCM. I was convinced and yes it is!

I have actually started SCM with svn( Apache Subversion). In fact started with TortoiseSVN, a GUI tool for Windows. Here there are no commands, no need to remember, so, nothing to worry, Just right click on your web root folder and choose whichever option you need! Sounds easy?

If you want to go with SVN, you can refer these links.
http://www.tutorialspoint.com/svn/svn_basic_concepts.htm
http://…

Categories: Elsewhere

Virtuoso Performance: DrupalCon NOLA Tuesday call to action - migration sprints

Wed, 11/05/2016 - 03:33
DrupalCon NOLA Tuesday call to action - migration sprints

Again, in the interests of timeliness I'll stick to a simple chronological wrapup of the day. And in the interests of of-course-everyone-cares-what-Mike-eats, I will continue subjecting you to my culinary adventures - breakfast at the Clover Grill in the midst of tourist land (Bourbon Street). Good, basic diner food - eggs over easy with bacon and hash browns, the primary goal here was to make it quick and get to the convention center in time for the prenote (which I have somehow never managed to rouse myself in time for at previous DrupalCons).

And, as always (by reputation), the prenote was an extravaganza hosted by jam. So much energy on the stage, so many songwriters calling their lawyers... Highlights were Gábor unveiling a sweet, soulful voice, and the epic Code of Conduct song (performed 1.5 times, so no excuses for not getting it down).

That brings us to - ta-da! - DriesNote. As always, a lot of information presented succintly - I'm sure others will cover many of his points, so I'll focus on my special interest - migration. In Dries' annual survey, site builders identified migration tools as their biggest need for Drupal 8, and he called out the Friday migration sprint.

Sprint all the migrates!

So... let's see how much progress we can make on core migration issues this week! Important things to note:

  1. You don't have to wait for Friday. The Sprint Lounge (rooms 275-277) is open every day. And, while as usual I checked off many, many sessions I'd like to attend, after sitting in a couple today where (through no fault of the presenters) I was mainly thinking about migration, I'm going to try to spend significant time every day (right up through Sunday morning) sprinting.
  2. You don't have to be in New Orleans! You can help remotely - drop into the #drupal-migrate IRC channel, or just pick issues from the core queue and dive in on your own.
  3. You don't have to know the migration framework - there are various ways you can help out (see below).

We already have 10 people officially signed up for migration sprinting (between the core and multilingual lists), so (particularly with more people joining) we can afford to split into multiple sprint teams:

  • Backwards-compability breakers - try to address any issues that may affect backwards-compability, so migration implementors will be able to count on a stable API from 8.2.x forward. This was my priority coming in, and you'll find triaged issues on the Sprint triage tab of the Migration sprint spreadsheet.
  • I18n issues - penyaskito is already leading a migration sprint in this area - it overlaps with the BC-breakers on the epic Migrate D6 i18n nodes issue.
  • Migrate criticals - note that this overlaps some with the BC-breakers (the BC-breaker list has its migrate-criticals listed first), so look for issues not already covered there.
  • UI issues - Abhishek Anand, who did some of the work on the UI in contrib, will lead efforts to clean up remaining issues in core. He'll be in the sprint room Wednesday morning, as well as most of the day Friday, and you can also coordinate with him outside of those times (or if you're not here).
  • We have a lot of issues at the Needs review stage - let's see how many we can get to RTBC, or give constructive feedback, so we can move forward on stuff like node and user references.
Specifically, how can I help?
  • If you're at DrupalCon NOLA, come to the sprint room (275-277) any time Wednesday-Friday - I'll try to get there early and reserve a table just for migration. There are a couple of sessions I definitely want to catch, but I should be there for most non-lunch time, and there should generally be others there (especially Friday) when I'm not.
  • If you're remote, you can announce your presence in #drupal-migrate on IRC. Or just pick an issue to work on.

Either way, please put your name under "Who's working on it" in the spreadsheet so we don't duplicate effort (multiple people can be involved in one patch, but should coordinate).

Ways to help on a specific issue:

  • Write a patch (or discuss approaches to a patch) where there is none yet.
  • Review an existing "needs review" patch.
  • Manually test a "needs review" patch - set up a patched D8 environment and try running your site through the migration process (we'll give some help on setup here).
  • Add tests to a patch tagged "Needs tests".
  • Help solve any outstanding issues on a "needs work" patch.
  • Any other ideas you might have...

 

 

mikeryan Tue, 05/10/2016 - 20:33 Tags
Categories: Elsewhere

Leopathu: Create a custom Twig filter in Drupal 8

Tue, 10/05/2016 - 22:06
Twig can be extended in many ways; you can add extra tags, filters, tests, operators, global variables, and functions. You can even extend the parser itself with node visitors. In this blog, I am going to show you how to create new custom twig filters in drupal. For example we are going to create a filter to remove numbers from string, will explain with hello_world module. Create hello_world folder in modules/custom/ folder with the following files,
Categories: Elsewhere

Jeff Geerling's Blog: Thoughts on the Acquia Certified Developer - Drupal 8 Exam

Tue, 10/05/2016 - 21:21

Another year, another Acquia Certification exam...

I'm at DrupalCon New Orleans, the first North American DrupalCon since the release of Drupal 8. In addition, this is the first DrupalCon where the Acquia Certified Developer - Drupal 8 Exam is being offered, so I decided to swing by the certification center (it's on the 3rd floor of the convention center, in case you want to take any of the certification exams this week!) and take it.

Categories: Elsewhere

LevelTen Interactive: LevelTen and Dries Keynote at DrupalCon New Orleans 2016

Tue, 10/05/2016 - 18:37

The LevelTen Team takes New Orleans! Read through our Twitter feed as we experience the opening ceremony of #DriesNote with the Drupal Community.

[View the story "Dries Note at DrupalCon New Orleans" on...Read more
Categories: Elsewhere

Four Kitchens: Web Chefs Wild at DrupalCon 2016

Tue, 10/05/2016 - 18:11

The Four Kitchens gang has landed— #4KNOLA! …

Categories: Elsewhere

Danny Englander: Drupal 8 Theming Tutorial: How to Craft Custom Theme Hook Suggestions and Templates

Tue, 10/05/2016 - 16:46

I've been theming with Drupal since version 6 back in 2009. In the intervening years, I've seen a lot of changes of how theming and front-end development is approached and have tried to keep up with the latest trends and adhere to best practices. Drupal 8 takes theming to a whole new level; it feels elegant, freeing, uniform, cohesive, and logical. So there's a lot to get excited about here.

In Drupal 8, there's templates for just about anything and with Drupal 8's new theme hooks, you can really narrow in on crafting a custom template for your specific use case. This makes getting at and altering markup far more accessible than it was in Drupal 6 and 7.

My preferred method for Drupal 8 theming is using Classy as my base theme, creating a sub-theme off of that and then overriding the parts that are necessary. An override is as simple as copying one of Classy's 100+ templates into your Classy based sub-theme. Sometimes, an out of the box template is not enough so you can create your own theme hook in that case.

In this post, I'll help you get started with creating Drupal 8 custom theme hook suggestions and templates. I also refer to various tools that I use, you can read more about them in my article, Drupal 8 Development: 5 Things to Add to Your Theming Toolbox.

Overview

The goal for this tutorial is to create a theme hook and subsequent template for a group of block regions in our site. The regions are called postscript_one, postscript_two, and postscript_three. The Drupal API function we will use for this is hook_theme_suggestions_HOOK_alter where "HOOK" is the name of the theme hook that we see in our Twig debug area. So essentially, we'll grab this theme hook and make a template suggestion for an array of these regions.

Getting Started

To start off, I'll place a block in one of my postscript regions and then examine the placed block with Web Inspector. Here is what I see when we do that:

<div class="postscript"> <!-- THEME DEBUG --> <!-- THEME HOOK: 'region' --> <!-- FILE NAME SUGGESTIONS: * region--postscript-second.html.twig x region.html.twig --> <!-- BEGIN OUTPUT from 'themes/custom/hibiscus/templates/layout/region.html.twig' -->

From the above, we see the theme hook name, region as well as a two template suggestions. What I am after here is a common region template for any of the the three postscript regions that I mentioned earlier. Time to fire up Devel Kint and see what we can learn by inspecting the region array. For this, I will initiate my theme hook function in my theme's .theme file. Of note is hibiscus, my Classy sub-theme theme name and the HOOK, "region".

/** * Implements hook_theme_suggestions_HOOK_alter(). */ function hibiscus_theme_suggestions_region_alter(&$suggestions, $vars, $hook) { }

Now I'll enable kint() as kint($vars);:

function hibiscus_theme_suggestions_region_alter(&$suggestions, $vars, $hook) { kint($vars); }

After I run drupal cr all using Drupal Console, I'll now see kint print out the region array on my page. As you can see from the below image.

Construct the theme hook

Using Kint search, we can search for and copy the path for the region name definition, $var['elements']['#region'] It is from this that we will construct our array of regions and subsequent theme hook. First I define the region name variable from the above:

$region = $vars['elements']['#region'];

Now I'll define an array of my three regions:

$region_postscript = array( 'postscript_first', 'postscript_second', 'postscript_third', );

Next I construct an if statement to make the actual theme suggestion:

if (in_array($region, $region_postscript)) { $suggestions[] = 'region' . '__' . 'postscript'; }

What this does is say, if it's any of the three postscript regions, create a theme hook that's common to all these regions.

Putting it all together

Putting the entire function all together, we now have:

/** * Implements hook_theme_suggestions_HOOK_alter(). */ function hibiscus_theme_suggestions_region_alter(&$suggestions, $vars, $hook) { // Define a variable for the #region name. $region = $vars['elements']['#region']; // Define an array of our postscript regions. $region_postscript = array( 'postscript_first', 'postscript_second', 'postscript_third', ); // If we are in any of these regions, create a theme hook suggestion. if (in_array($region, $region_postscript)) { $suggestions[] = 'region' . '__' . 'postscript'; } }

Now that I have written my theme hook, I can clear cache and reload the page to see my new theme hook suggestion in action.

<!-- FILE NAME SUGGESTIONS: * region--postscript.html.twig * region--postscript-second.html.twig x region.html.twig

We now see out new template suggestion from our custom theme hook, region--postscript.html.twig. This template would be common to all three regions so we can alter or update the markup in this new template as well as add classes as we see fit. Since I have a Classy sub-theme in my case, I copy Classy's region.html.twig and rename it to the new name within my own sub-theme folder structure. Of course if you are using stable as your base theme, you would do the same thing copying stable's template of the same name.

Summary

This is just a basic example to show what's possible with creating Drupal 8 theme hooks and template suggestions. Hopefully you can see that it's really useful and should come in handy for themers.

Tags 
  • Drupal Planet
  • Drupal
  • Preprocess
  • Drupal 8
  • Twig
  • Theming
Resources 
Categories: Elsewhere

OSTraining: Drupal 8 console & Composer faster site development

Tue, 10/05/2016 - 14:53

One of our OSTraining members asked what was the best way to install composer to manage dependencies.

While composer can be used to add module dependencies into drupal. It's more practical for DrupalConsole to handle the process. Console automatically passes the requests to composer. 

From the terminal command line running the following commands will install both Console and Composer together.

Categories: Elsewhere

Web Wash: How to Create a Search Page in Drupal 8

Tue, 10/05/2016 - 14:24

Building a search page isn't as straight forward as you'd think. At first a client will want something which users can search content, then they may want to modify the search results or even change the ranking of certain content. Long story short, something you thought would be as simple as enabling a module, ends up taking twice as long.

If you need to create custom search pages in Drupal 7, more often than not, you use [Search API](https://www.drupal.org/project/search_api) or create a [search page using Views](https://www.webwash.net/node/84). But the core Search module for Drupal 8 has become more powerful than in Drupal 7. One of the big changes, for site builders, in Drupal 8 is the ability to create custom search pages.

However, there're a few limitations to creating a search page. First, it'll have a prefix of "search/" in front but the full URL can be changed by creating a URL alias. Second, you can only adjust the content ranking on these pages. If you want to index extra fields or remove ones from being indexed, you'll still need [Search API](https://www.drupal.org/project/search_api) to do this.

In this tutorial, you'll learn how to create a custom search page and how to modify the search results by overriding a template.

Categories: Elsewhere

Drupal core announcements: Core topic discussions at DrupalCon New Orleans

Tue, 10/05/2016 - 14:12

DrupalCon New Orleans includes a full track of core conversations where you can learn about current topics in Drupal core development and participate in shaping Drupal's future.

In addition to the core conversations, we have a few meetings on specific topics for future core development. These meetings will be very focused, so contact the listed organizer for each if you are interested in participating.

Time Topic Organizer Tuesday May 10, 10:00a-12:00p Content Workflow dixon_ Thursday May 12, 12:00p-2:00p REST/API-first improvements prestonso Thursday May 12, 4:30-6:00p Theme component library Wim Leers Friday May 13, 11:00a-12:00p Migrate critical triage (round 2) mikeryan Friday May 13, 12:30p-2:00p Blocks and Layouts tim.plunkett

Also be sure to watch Dries' keynote for ideas about Drupal's future!

Categories: Elsewhere

Blair Wadman: Resolving "Fatal error: class not found" in Drupal 7

Tue, 10/05/2016 - 11:18

You have gone through your regular module update routine and cleared the cache. Suddenly you are presented with a PHP Fatal error, because a class can’t be found. Your Drupal site won’t load and you can’t run Drush commands. All you get is the fatal error blocking the site.

If you get a fatal error like above where a class is not found, a likely cause is that the Drupal registry has the old path for the class. A cache clear should update, but sometimes it all goes a bit wrong. The solution is to rebuild the registry. This will update the registry table with the new file locations. Read on to find out how...

Categories: Elsewhere

Web Omelette: Query for nodes which reference multiple terms in Drupal 7

Tue, 10/05/2016 - 09:05

Have you ever needed to run a custom query that returns all the nodes which reference two or more taxonomy terms? I have, and most of the time it was very simple because the requirement was for nodes which reference either term, rather than ALL the terms.

When we are dealing with an OR type of select query, it couldn't be easier:

$query = db_select('node', 'n'); $query->join('taxonomy_index', 'ti', 'ti.nid = n.nid'); $query->condition('ti.tid', array(1, 2)); $query->fields('n', array('nid')); $result = $query->execute();

The above example keeps things simple by assuming the term reference maintains an index in the taxonomy_index table and that we already have our term IDs.

Things get a bit more complicated when we are trying to query the nodes which reference both term 1 AND term 2. After banging my head against the wall for a bit, I came up with a solution inspired by how Views generates its query:

$tids = array(1, 2); $query = db_select('node', 'n'); foreach ($tids as $key => $tid) { $query->innerJoin('taxonomy_index', 'ti_' . $key, 'ti_' . $key . '.nid = n.nid AND ti_' . $key . '.tid = ' . $tid); } foreach ($tids as $key => $tid) { $query->condition('ti_' . $key . '.tid', $tid); } $query->fields('n', array('nid')); $result = $query->execute();

So basically the solution is to create a join for each term ID we want to filter by. A crucial part of this is that the join needs to happen on the node ID between the two tables (as expected) but also on the term ID we are using for the filter matching the record in the taxonomy_index table. This will ensure that the two joins are not the same but that they reflect the relation between the node record and each individual term ID. Are you still following? Then, we just add our conditions on the newly created joins.

Caveats

This should work, and depending on the size of your dataset, it should not have too much of a performance impact. However, as no joins should be done if not absolutely necessary, investigate the possibility of querying for the nodes that reference the first term and then filtering the rest out using PHP. This can be a viable option if you know that usual result sets are not too big so you are not running array_filter() on 2000 terms. And of course, when possible, cache the query appropriately.

Categories: Elsewhere

Pages