Implementation of conditions on Drupal AND OR queries

A simple query on Drupal looks like:

 
$query = db_select('users', 'u', $options);

So, a full query is:

 
//The query using MySQL
$result = db_query("SELECT uid, name, status, created, access FROM {users} u WHERE uid <--> 0 LIMIT 50 OFFSET 0");

//Using Drupal Queries
// Create an object of type SelectQuery
$query = db_select('users', 'u');

// Add extra detail to this query object: a condition, fields and a range
$query->condition('u.uid', 0, '<>');
$query->fields('u', array('uid', 'name', 'status', 'created', 'access'));
$query->range(0, 50);

?>


To include AND conditions:

AND conditions:

 
$query=db_select('users','u')->fields('u',array('uid','title','created','uid'));
$query->join('flag_content','fc' , 'u.uid = fc.content_id');
$query->condition('fc.fid', '5' , '=');
$query->condition('fc.uid', $uid , '=');

OR conditions:

 

$query=db_select('users','u')->fields('u',array('uid','title','created','uid'));
$query->join('flag_content','fc' , 'u.uid = fc.content_id');

$db_or = db_or();
$db_or->condition('fc.fid', '5' , '=');
$db_or->condition('fc.uid', $uid , '=');
$query->condition($db_or);

$result = $query->execute()->fetchAll();

Save time using Drush on Drupal

Drush is a command line shell and scripting interface for Drupal, a veritable Swiss Army knife designed to make life easier for those of us who spend some of our working hours hacking away at the command prompt.

Simple tasks like clear cache can take long time using the web interface. Using drus you just need to use the following command:

drush cc

Ant It will display something like this:

 
Enter a number to choose which cache to clear.
 [0]   :  Cancel
 [1]   :  all
 [2]   :  drush
 [3]   :  theme-registry
 [4]   :  menu
 [5]   :  css-js
 [6]   :  block
 [7]   :  module-list
 [8]   :  theme-list
 [9]   :  registry
 [10]  :  token
 [11]  :  views

From this moment you can select if you want to clear the cache of something specific or All.

Other option very useful it to download modules for that you follow this command:

drush dl module_name

There are many other options for drush, you can find at the project page:

https://drupal.org/project/drush

How to create a calendar for a content type on Drupal

You just need to install the calendar module. Located on this url:

https://drupal.org/project/calendar

After install it you will have access to some new views as a template:[[{“type”:”media”,”view_mode”:”media_original”,”fid”:”35″,”attributes”:{“alt”:””,”class”:”media-image”,”height”:”620″,”style”:”line-height: 1.538em;”,”typeof”:”foaf:Image”,”width”:”1359″}}]]

 

If you use any of them It will show you the Views interface where you will be able to select the fields for consideration for the content type you want to display the calendar:

[[{“type”:”media”,”view_mode”:”media_original”,”fid”:”36″,”attributes”:{“alt”:””,”class”:”media-image”,”height”:”647″,”typeof”:”foaf:Image”,”width”:”773″}}]]

 

Get Values from field collection on Drupal 7

Let’s supose you have an user with a field collection named “field_certification” and you have a field that references to a field called field_class_type.

$myuser = user_load($user->uid);
//Get certifications ids without entity interpretation
$certifications_array = $myuser->field_certification[LANGUAGE_NONE];
 

In $certifications_array you will obtain ids taht do not match the terms, so you need to conver them to get the correct value:

//Convert obtained ids to terms
$certifications = array();
foreach($certifications_array as $certification){
$entity = entity_load('field_collection_item', array($certification['value']));

if(!isset($entity[$certification['value']]->field_class_type[LANGUAGE_NONE])) continue;

$term_id = $entity[$certification['value']]->field_class_type[LANGUAGE_NONE][0]['tid'];

if(!empty($term_id))
$certifications[] = $term_id;
}