Wednesday, 13 July 2016

How to add select event to auto complete functionality in Drupal?

(function ($) {
  Drupal.behaviors.autocompleteSupervisor = {
    attach: function (context) {
      $("input#edit-autocompleteField", context).bind('autocompleteSelect', function(event, node) {
        console.log($(this).val()); // user-entered string
        console.log($(node).data('autocompleteValue')); // key of selected item
        console.log($(node).text()); // label of selected item
      });
    }
  };
})(jQuery);

How to add auto complete feature to text field in node form Drupal?

if ($form_id == 'test_node_form') {
        foreach($form['field_test']['und'] as $key => $value) {
            if (is_numeric($key)) {
                    $form['field_test']['und'][$key]['value']['#autocomplete_path'] = 'autosuggest-path';                    
            }
        }            
    }

How to get View result programatically by passing exposed filter inputs in Drupal?

    $view = views_get_view('view_name');
    $view->exposed_input['keyword'] = $searchtext;
    $view->set_items_per_page(4);
    $view->execute();
    $view->preview();
    $results = $view->result;

How to add IMCE Browser to Multi Image Upload Field in Drupal 7?

We need to alter the UI of image_miw widget and add the checkbox to select IMCE under the manage fields section of Content Type
/**
 * Implements hook_form_FORM_ID_alter().
 */
function custom_form_field_ui_field_edit_form_alter(&$form, &$form_state) {
  $instance = $form['#instance'];
  if ($instance['widget']['type'] == 'image_miw') {
    $form['instance']['widget']['settings'] += array('imce_filefield_on' => array(
      '#type' => 'checkbox',
      '#title' => t('Allow users to select files from <a href="!url">IMCE File Browser</a> for this field.', array('!url' => url('admin/config/media/imce'))),
      '#default_value' => $instance['widget']['settings']['imce_filefield_on'],
      '#weight' => 16,
    ));
  }
}
/**
 * Implements hook_field_widget_info_alter().
 */
function custom_field_widget_info_alter(&$info) {
  if (isset($info['image_miw'])) {
    $info['image_miw']['settings']['imce_filefield_on'] = 0;
  }
}
Add IMCE Field Process functions to  mfw_managed_file Field
/**
 * Implements hook_element_info().
 */
function custom_element_info() {
  $elements = array();
  $elements['mfw_managed_file']['#process'] = array('imce_filefield_field_process');
  $elements['mfw_managed_file']['#pre_render'] = array('imce_filefield_field_pre_render');
  $elements['mfw_managed_file']['#file_value_callbacks'] = array('imce_filefield_field_value');
  return $elements;
}

How to show content through Views in Drupal 7 from separate Database table?

/**
 * Implementation of hook_views_api
 */
function commportal_search_views_api() {
   return array(
      'api' => 3,
   );
}
/**
 * Implementation of hook_views_data
* custom is Module Name
custom_users is Table Name
* my_db is Database Name
 */
function  custom_views_data() {
    $data['custom_users']['table']['group'] = 'custom_users';
        $data['custom_users']['table']['base'] = array(
          // Use the first column's name as the primary field.
          'field' => 'id',
          'title' => 'custom_users',
          'database' => 'my_db',
          'weight' => -9001,
        );
/* Add Numeric Field Handlers */
    $numericFields = array('id', 'roll_number');
    foreach($numericFields as $fieldId){
        $data['custom_users'][$fieldId] = array(
            'title' => $fieldId,
            'help' => $fieldId,
            'field' => array(
            'handler' => 'views_handler_field_numeric',
                'click sortable' => TRUE,
            ),
            'sort' => array(
                'handler' => 'views_handler_sort',
            ),
            'filter' => array(
                'handler' => 'views_handler_filter_numeric',
            ),
            'argument' => array(
                'handler' => 'views_handler_argument_numeric',
            ),
        );
    }
/* Add String Field Handlers */
    $stringFields = array('email', 'first_name', 'last_name');
    foreach($stringFields as $fieldId){
        $data['custom_users'][$fieldId] = array(
            'title' => $fieldId,
            'help' => $fieldId,
            'field' => array(
            'handler' => 'views_handler_field',
                'click sortable' => TRUE,
            ),
            'sort' => array(
                'handler' => 'views_handler_sort',
            ),
            'filter' => array(
                'handler' => 'views_handler_filter_string',
            ),
            'argument' => array(
                'handler' => 'views_handler_argument_string',
            ),
        );
    }
   return $data;
}

How to Order the Select Query results based on Search Text Expression in Drupal 7?

Let us consider as user contents are stored in a indexed field "associatedetails" as below:
Row1 Contains : "AjayKumar Gudivada";
Row2 Contains : "Ajay Kumar Gudivada";
Row3 Contains : "Gudivada";
Now, if a user searches for text "Kumar", we need to show only the results where the searched text exists. And show the results where the searched text matches starting of word in priority and display the rest.
$searchtext = "Kumar";
$query = db_select('custom_users', 'u');
    $query->fields('u', array('email', 'phone_number', 'mobile_number', 'work_location'));
    $c = db_or()
        ->condition('associatedetails', db_like($searchtext) . '%', 'LIKE')
        ->condition('associatedetails', '% ' . db_like($searchtext) . '%', 'LIKE');
    $query->condition('status', 1, '=');
    $query->condition($c);
    $query->addExpression('CASE WHEN associatedetails LIKE :db_condition_placeholder_1 THEN 2 WHEN associatedetails LIKE :db_condition_placeholder_2 THEN 1 ELSE 0 END','order_col');
    $query->orderBy('order_col','DESC');
    $query->range(0, 10);
    $result = $query->execute();
Now, the results will show as Row2 in first position and Row1 in second position.

How to add Mobile Responsive Preview Links to Administration Menu in Drupal 7?

Need to enable Responsive Preview module and use hook_admin_menu_output_build() as below:
/**
 * Add responsive preview links to the Admin Menu.
 */
function custom_admin_menu_output_build(&$content){
    $content['responsive_preview'] = array(
        'device_options' => array(
                '#theme' => 'item_list',
                '#items' => responsive_preview_get_devices_list(),
                '#attributes' => array(
                    'id' => 'responsive-preview-navbar-tab',
                    'class' => array('responsive-preview-options'),
                ),
            ),
        '#wrapper_attributes' => array(
            'class' => array('navbar-tab-responsive-preview'), // Class Name is important as Responsive Preview JS will work based on the class name
        ),
        '#weight' => 200,
        '#access' => responsive_preview_access(),
    );
}
And, add Responsive Preview library in hook_init() function
function custom_init() {
    drupal_add_library('responsive_preview', 'responsive-preview');
}