Tuesday 31 March 2015

Drupal 7 Form API - Reveal Input on drop down condition

Here’ the task :  I have a drop down list , a simple ‘yes’ or ‘no’  .  If yes fill in the the input box. 

At first I thought this would need a simple Ajax form call but in fact its even easier than that.   We can use the ‘States’ and ‘visible’ values of the Form API [https://api.drupal.org/api/examples/form_example%21form_example_states.inc/function/form_example_states_form/7]

Here’s my code that solved this issue

$form['addtoportfolio'] = array(
  '#type' => 'select',
  '#prefix' => "Your score was $score%; the pass rate is $pass_rate% ",
  '#title' => t('Would you like to add this to your portfolio ?'),
  '#required' => TRUE,
  '#options' => array(
    'n' => t('No'),
    'y' => t('Yes'),
  ),
  '#default_value' => t('No'),
  );



$form['timetaken'] = array(
  '#type' => 'textfield',
  '#title' => t('Time Taken ?'),
  '#required' => FALSE,
  '#description' => "Please enter the time in minutes that it took you to do this quiz.",
  '#size' => 4,
  '#maxlength' => 10,
  '#states' => array(
    'visible' => array(
      ':input[name="addtoportfolio"]' => array('value' => 'y'),
    ),
  ),

);

Friday 27 March 2015

Drupal 7 how to save values to an Entity in Form Submit.

Yes you can add a field with a db_query like this

time_taken_updated = db_update('quiz_node_results')
 ->fields(array(
 'time_taken' => $time_taken ,
 ))
 ->condition('result_id', $result_id, '=')
 ->execute();


but it's better to do it with Entity_Metadata_wrapper in your hook_form_submit

like


$saq_results = entity_load('quiz_result', array($result_id));

$saq_results_wrapper = entity_metadata_wrapper('quiz_result', $saq_results);

$saq_results_wrapper->time_taken->set($time_taken);

Wednesday 25 March 2015

Modular CSS with Sass notes.


The @content directive lets us pass custom styles to a mixin when including it.


With SMACSS, element state selectors are usually prefixed with .is- 

The @at-root directive lets us nest a Sass rule without nesting the output.

In a conditional mixin, which directives are used to set the conditions? 
if and else


Now onto that last question 


Add the statement that instructs @at-root to output the nested state rule outside the @media query.  Anyone know the answer?

Friday 20 March 2015

Drupal 7 Theme TouchM make Portfolio Images Link to Node or Page.



> Copy these two pages from sites/all/modules/tabvn/portfolio/    
  1. portfolio_4c.tpl.php
  2. portfolio_filter.tpl.php

>  Paste them into sites/all/themes/touchm/templates

> OPEN 

/sites/all/themes/custom/touchm/templates/portfolio_4c.tpl.php

CHANGE LINE 35 to . 











Thursday 19 March 2015

Drupal 7 Views Display SQL Query

How to display the SQL query on Views.

admin/structure/views/settings  tick ‘Show the SQL query’

Friday 13 March 2015

Adding OG Tags to a Drupal 7 website

)pen Graph tags are tags that are used to grab content by some of the major Social Medial site. 


I’ve added code to the themes template.php that will display this .


The main issue here was the 'image' tag which I've cross referenced the database for . These fields will be different for others ; depending on the tables that store your images.


/** * Implements template_preprocess_html() * * @param $vars *   An array of variables to pass to the theme template. *   The name of the template being rendered ("html" in this case.) */
function theme_preprocess_html(&$vars) {

  /* * @ file Open Graph Meta tags - for LinkedIn and Facebook * * The following code writes the relevant metatags required for Open Graph. * check 'open graph' on  https://developer.linkedin.com/docs/share-on-linkedin * for more information * There's 2 images in the node that we can add  1. field_image and 2. field_header_image * We'll try field header image first. * Lets get the node id. */  global $base_url;


  $og_url = "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
  $site_name = variable_get('site_name');
  $og_title = $vars['node']->title . ($site_name ? ' | ' . $site_name : '');
  $od_dec = $vars['page']['content']['metatags']['node:flexible_article']['description']['#attached']['drupal_add_html_head'][0][0]['#value'];
  $page_node_attribute = $vars['attributes_array']['class']['6']; //  $page_node_attribute_array = explode('-', $page_node_attribute);
  $nid = end($page_node_attribute_array);

  drupal_add_html_head(array(
    '#tag' => 'meta',
    '#attributes' => array(
      'property' => 'og:title',
      'content' => $og_title,
    ),
  ), 'node_' . $nid . '_og_title');

  drupal_add_html_head(array(
    '#tag' => 'meta',
    '#attributes' => array(
      'property' => 'og:description',
      'content' => $od_dec,
    ),
  ), 'node_' . $nid . '_og_description');

  drupal_add_html_head(array(
    '#tag' => 'meta',
    '#attributes' => array(
      'property' => 'og:url',
      'content' => $og_url,
    ),
  ), 'node_' . $nid . '_og_url');


  $result = db_query('SELECT fm.filenameFROM {field_data_field_header_image} fhi LEFT JOIN {file_managed} fm ON fhi.field_header_image_fid = fm.fid  WHERE fhi.entity_id = :nid  LIMIT 1', array(
    ':nid' => $nid,
  ));
  $image_file = $base_url . '/';
  foreach ($result as $record) {
    $filename = $record->filename;
    $filename_fixed = str_replace(' ', '%20', $filename);
    $image_file .= 'sites/default/files/styles/article_header/public/header_images/' . $filename_fixed;
  }

  if (!isset($filename) && $filename == NULL) {
    $result2 = db_query('SELECT fm.filenameFROM {field_data_field_image} fhi LEFT JOIN {file_managed} fm ON fi.field_image_fid = fm.fid  WHERE fi.entity_id = :nid  LIMIT 1', array(
      ':nid' => $nid,
    ));
    $image_file = $base_url . '/';
    foreach ($result2 as $record) {
      $filename = $record->filename;
      $filename_fixed = str_replace(' ', '%20', $filename);
      $image_file .= 'sites/default/files/styles/article_header/public/header_images/' . $filename_fixed;
    }
  }

  if ($filename != NULL) {
    drupal_add_html_head(array(
      '#tag' => 'meta',
      '#attributes' => array(
        'property' => 'og:image',
        'content' => $image_file,
      ),
    ), 'node_' . $nid . '_og_image');

  }
}

This code was inspired by http://kahthong.com/2012/08/add-open-graph-protocol-meta-data-your-drupal-node-page

Thursday 5 March 2015

Some tips on checking your code in Drupal

Before I start it's worthy of note that this is not really a blog but more of a place where I copy and paste my own notes on things.  If the following is of help to anyone then thats great also.  Conversation on better techniques are also more than welcome.

There’s  a handful of different methods for checking your code.  One way is to run the ‘Code Sniffer’ from inside PHP Storm .  This is a great way to keep a running score of anything you might need to fix. 

As well as John’s notes on this here . 


and the jet brains instruction on this is here.  


Check in Project Settings > inspections > Enable ‘PHP Code Sniffer validation'












From here on you get a list of suggested changes on the right hand side.  You can see these by clicking on the yellow box and then running down the righthand side are some yellow markers; click on these for suggested code changes needed. 



Another way though is to install project/coder module on your drupal installation .  I find this easier to use; especially if you’re reviewing a module.