PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites

29 Dec 2015

Drupal 7 - Add custom fields to Search API solr index

     This is blog describes about how to add custom field to Search API solr index in Drupal 7.

     Suppose we need a new field, we can add new fields for a content type at /admin/structure/content/types. Then all fields are showed at /admin/config/search/search_api/index/default_node_index/fields. Now you can add desired fields to solr index.

     Suppose you want to show custom field to Search API results but that field is not created for any specific content types. Is it possible in Search API? Yes you can done this with use of hook_entity_property_info_alter().

/**
 * Implement phponwebsites_get_nodecountviews_nid()
 *
 * Return count of views by particular nid
 */
function phponwebsites_get_nodecountviews_nid($nid) {
  $result = db_query("SELECT COUNT(*) as count FROM {nodeviewcount} WHERE nid=:nid", array(':nid' => $nid))->FetchAssoc();
  return $result['count'];
}

/**
 * Implements hook_entity_property_info_alter().
 */
function phponwebsites_entity_property_info_alter(&$info) {
  $info['node']['properties']['is_nodeviewcount'] = array(
    'type' => 'integer',
    'label' => t('Node view count'),
    'description' => t("Number of views."),
    'sanitized' => TRUE,
    'getter callback' => 'phponwebsites_get_is_nodeviewcount_callback',
  );
}

/**
 * Implement phponwebsites_get_is_nodeviewcount_callback()
 */
function phponwebsites_get_is_nodeviewcount_callback($item) {
  $count = phponwebsites_get_nodecountviews_nid($item->nid);
  $total = (int) $count;
  return $total;
}

          After added above code into your custom module, go to /admin/config/search/search_api/index/default_node_index/fields. Now you could see new custom field is displayed as in below images.


Add custom fields to search API solr index in Drupal 7


         Now you can add your custom field into search api solr index and index that field. The custom field is listed in views add field section. Now you can add custom field into search results.

22 Dec 2015

Drupal 7 - Create menu tab programmatically

    This blog describes about how to create menu tab programmatically in drupal 7. We can create menu items using hook_menu().

Create menu tab programmatically

Menu tab creation in Drupal 7:


     Consider below code snippet to create menu tab in drupal 7.

/**
 * Implement hook_menu()
 */
function phponwebsites_menu() {

  $items['test'] = array(
    'title' => t('Create Menu Tab'),
    'page callback' => 'testpage_tab1',
    'access callback' => TRUE,
  );

  $items['test/tab1'] = array(
    'title' => t('First Tab'),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'page callback' => 'testpage_tab1',
    'access callback' => TRUE,
  );

  $items['test/tab2'] = array(
    'title' => t('Second Tab'),
    'type' => MENU_LOCAL_TASK,
    'page callback' => 'testpage_tab2',
    'access callback' => TRUE,
  );

  $items['test/tab3'] = array(
    'title' => t('Third Tab'),
    'type' => MENU_LOCAL_TASK,
    'page callback' => 'testpage_tab3',
    'access callback' => TRUE,
  );

  return $items;
}

/**
 * Implement testpage_tab1()
 */
function testpage_tab1() {
  $str = t('Hi this is first tab');
  return $str;
}

/**
 * Implement testpage_tab2()
 */
function testpage_tab2() {
  $str = t('Hi this is second tab');
  return $str;
}

/**
 * Implement testpage_tab3()
 */
function testpage_tab3() {
  $str = t('Hi this is third tab');
  return $str;
}


Where,
   In hook_menu,
      Title – page title
      Type – type of menu item,
       MENU_CALLBACK, MENU_DEFAULT_LOCAL_TASK, MENU_LOCAL_TASK, MENU_LOCAL_ACTION, MENU_NORMAL_ITEM, MENU_SUGGESTED_ITEM are types of
menu item in drupal 7.

MENU_CALLBACK – register path for a menu item
MENU_DEFAULT_LOCAL_TASK – default tab for a menu
MENU_LOCAL_TASK – additional tabs for menu
MENU_LOCAL_ACTION – actions for menu items
MENU_NORMAL_ITEM – add menu item into any menus like main_menu, user_menu
MENU_SUGGESTED_ITEM – module may suggest menu items

Page callback – callback for a menu
Access callback – who can access the page

        Now i’ve hope you should know how to create menu tab programmatically in drupal 7.