Wednesday, 13 July 2016

How to sort Views results based on Content Type display name instead of machine name in Drupal 7?

We can join 'node' table with 'node_type' table in views_query_alter.  And, add the Node Type Display Name to the fields list so that it can be used to sort.
$join = new views_join;
        $join->construct('node_type',
                    'node',  // left table
                    'type',   // left field
                    'type'   // field
            );
        // Add join to query; 'node' is the left table name
        $view->query->add_relationship('node_type', $join, 'node');
        // Add fields from table (or where clause, or whatever)
        $view->query->add_field('node_type', 'name', 'type_name');
        $orderby = $view->query->orderby;
        if(isset($orderby[0]['field']) && $orderby[0]['field'] == 'node_type'){
        $view->query->orderby[0]['field'] = 'type_name';
        }

How to sort a Multidimensional Array based on a value in PHP?

Let's consider $contents is the Multidimensional Array which needs to get sort based on a 'info' value
$sorted_data = array();
    foreach ($contents as $key => $content) {
    $sorted_data[$key]  = $content['info'];
    }
array_multisort($sorted_data, SORT_ASC, $contents);
Here, array_multisort will sort $contents variable based on 'info' value.

How to get Search API DB results?

We can use search_api_query function to get the results by passing index machine name and search key word as follows:
$index = 'nodes_index';
$keyword = 'learn subjects';
$query = search_api_query($index);
$query->keys($keyword);
$query->range(0,4);
$results = $query->execute();

Saturday, 30 May 2015

How to check whether a region contains block for current page?

At some cases, we might need to add some div structures to a group of regions, at that case, we need to check whether region has any block or not to place the div structure.
 
Ex:
Code in page.tpl.php
 
<?php if(region_empty($testRegion1)) { 
?>
<div id="testId">
<?php
print $testRegion1;
 print $testRegion2;
 print $testRegion3; 
 print $testRegion4;
?>
</div>
<?php 
}
?>
The function region_empty() would return 1 if it has a block to show.
<?php
function region_empty($test_region) {
  $test_empty = 1;
  $result = db_query_range('SELECT n.pages, n.visibility FROM {blocks} n WHERE n.region="%s" AND n.theme="%s"', $test_region, $GLOBALS['theme'], 0, 10);
  if (count($result) > 0) {
    while ($node = db_fetch_object($result))
    {
 
      if ($node->visibility < 2) {
        $path = drupal_get_path_alias($_GET['q']);
 
        // Compare with the internal and path alias (if any).
        $page_match = drupal_match_path($path, $node->pages);
        if ($path != $_GET['q']) {
          $page_match = $page_match || drupal_match_path($_GET['q'], $node->pages);
        }
        $page_match = !($node->visibility xor $page_match);
      } else {
        $page_match = drupal_eval($block->pages);
      }
 
      if ($page_match)
        $test_empty = 0;
    }
  }
  return $test_empty;
}
?>

How to add custom styles to form elements in template.php file?

Ex: 
/*To add styles to form elements in template.php file*/
function theme_form_element($element, $value) {
// This is also used in the installer, pre-database setup.
$t = get_t();
$output = '<div class="form-item"';
if (!empty($element['#id'])) {
$output .= ' id="'. $element['#id'] .'-wrapper"';
}
$output .= ">\n";
$required = !empty($element['#required']) ? '<span class="form-required" title="'. $t('This field is required.') .'">* </span>' : '';
if (!empty($element['#title'])) {
$title = $element['#title'];
if (!empty($element['#id'])) {
$output .= ' <label for="'. $element['#id'] .'">'. $t('!title !required', array('!title' => filter_xss_admin($title), '!required' => $required)) ."</label>\n";
}
else {
$output .= ' <label>'. $t('!title: !required', array('!title' => filter_xss_admin($title), '!required' => $required)) ."</label>\n";
}
}
$output .= " $value\n";
if (!empty($element['#description'])) {
$output .= ' <div class="description">'. $element['#description'] ."</div>\n";
}
$output .= "</div>\n";
return $output;
}
 
/* It would be possible to add styles to individual elements*/
function theme_button($element) {
   if (isset($element['#attributes']['class'])) {
    $element['#attributes']['class'] = 'form-'. $element['#button_type'] .' '. $element['#attributes']['class'];
  }
  else {
    $element['#attributes']['class'] = 'form-'. $element['#button_type'];
  }
  return '<input type="'.(isset($element['#button_type']) ? $element['#button_type'] : "submit").'" '. (empty($element['#name']) ? '' : 'name="'. $element['#name'] .'" ') .'id="'. $element['#id'] .'" value="'. check_plain($element['#value']) .'" '. drupal_attributes($element['#attributes']) ." />\n";
}
 
 
/* For Fieldset */
function phptemplate_fieldset($element) {
  if ($element['#collapsible']) {
    drupal_add_js('misc/collapse.js');
 
    if (!isset($element['#attributes']['class'])) {
      $element['#attributes']['class'] = '';
    }
 
    $element['#attributes']['class'] .= ' collapsible';
    if ($element['#collapsed']) {
     $element['#attributes']['class'] .= ' collapsed';
    }
  }
  return '<fieldset'. drupal_attributes($element['#attributes']) .'>'. ($element['#title'] ? '<legend>'. $element['#title'] .'</legend>' : '') .'<div class="top"><div class="bottom"><div class="bottom-ornament">'. (isset($element['#description']) && $element['#description'] ? '<div class="description">'. $element['#description'] .'</div>' : '') . (!empty($element['#children']) ? $element['#children'] : '') . $element['#value'] ."</div></div></div></fieldset>\n";
}

Added fields to content type, selected input type to allow PHP, however, while printing it, the PHP things are not getting rendered, any solution?

We have to use drupal_eval() in that case.
This function will render the PHP data.

When moved contents from one site to another, some contents were not came in the list, what would be the reason?

DRUPAL would check the authorization while retreving the content.
Hence, if the author is not available in the site, it wouldn't show the associated content.
Ex:
Dev environment logins wont be available in QA environment.  Hence, it would be good to change the author name to admin or make it blank  while creating contents if planned to move the content.