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. 

Thursday, 26 February 2015

Drupal 7 views search api add displaying count

If you'd like to let your users know how many search terms there has been on a search then .


Go to the View you are using for the search page .


administration > Structure > Views


> go to the display you are on  ie 'page'

> in the middle panel find 'HEADER'

> click add

> find 'Global: Results summary'

>  this will be pre filled with 'Displaying @start - @end of @total'  which you can change and add extra tokens if you need.

Monday, 23 February 2015

drupal 7 search api facets change Show more text

Go to > Administration > Configuration > Search and Metadata


Edit >  'Facet "more" link text

And Edit 'Facet "fewer" link text 
 ‘ 


SAVE 

Friday, 20 February 2015

drupal 7 how to make a view from indexed search


drupal create a search view for indexed node solr

The key here is to choose the name of the search index from the ‘show’ drop down box when you ‘Add a new view’  .  Check this page for full instructions. https://www.drupal.org/node/1597930 

Tuesday, 3 February 2015

MAMP Pro issue with changing php.ini files

The issue here is caused by trying to be too clever and changing php.ini files in the address given in you php info.

when what you need to do is use the MAMP Pro UI


 MAMP PRO > FIle > Edit Template  > php.ini > your default