0

At first I thought this was a job for hook_preprocess_field(); however, it then occurred to me that this field is populated by an ajax look up.

Where might be the best place to remove the group id from this? That is, how do I remove the (2) from the "TEST GROUP, Private" result pictured below?

I keep having users ask me why it's there.

enter image description here

I've also explored this in the Firefox Inspector. When I key in test, I see that requests are being sent to /og/autocomplete/tags/og_group_ref/node/post/default/NULL/test

And going a bit of work, I see that that path maps to these callbacks:

mysql> select path, page_callback from menu_router where path like 'og%';
+--------------------------------+------------------------------------------+
| path                           | page_callback                            |
+--------------------------------+------------------------------------------+
| og/autocomplete/single/%/%/%/% | og_entityreference_autocomplete_callback |
| og/autocomplete/tags/%/%/%/%   | og_entityreference_autocomplete_callback |
+--------------------------------+------------------------------------------+
2 rows in set (0.00 sec)

Which I find in these files:

$ grep -Rin 'og_entityreference_autocomplete_callback' *
profiles/commons/modules/contrib/og/includes/og.field.inc:313:function og_entityreference_autocomplete_callback($type, $field_name, $entity_type, $bundle_name, $field_mode, $entity_id = '', $string = '') {
profiles/commons/modules/contrib/og/og.module:100:    'page callback' => 'og_entityreference_autocomplete_callback',
profiles/commons/modules/contrib/og/og.module:107:    'page callback' => 'og_entityreference_autocomplete_callback',

And the callback function returns entityreference_autocomplete_callback_get_matches.

2 Answers 2

0

Well, I changed the form field og_group_ref to a select box. This is enough to hide the group id number from users and has curbed confusion. Though I'd be happy to hear what others would do in this situation.

enter image description here

0

For Drupal 7 Commons we can override it using hook_menu_alter and hook_form_alter with the copy of OG and Entity reference functions. Also need a DB query to get the reference ID(nid) using the Group Title.

  • hook_menu_alter

    • Alter OG autocomplete page callbacks
      
      /**
      *Implements hook_menu_alter
      */
      function hook_menu_alter(&$items) {
              $items['og/autocomplete/single/%/%/%/%']['page callback'] = 'override_entityreference_autocomplete_callback';
              $items['og/autocomplete/tags/%/%/%/%']['page callback'] = 'override_entityreference_autocomplete_callback';
      }
      
  • hook_form_alter

    • Add custom form validation handler

      
      /**
      *Implement hook_form_alter()
      */
      function hook_form_alter(&$form, &$form_state, $form_id) {
              switch ($form_id) {//add your forms based on your requirement
                      case 'post_node_form':
                      case 'poll_node_form':
                      $value = preg_replace('/\s+\((\d+)\)/', '', $form['og_group_ref'][LANGUAGE_NONE][0]['default']['#default_value']);// On page load remove entity ID
                      $form['og_group_ref'][LANGUAGE_NONE][0]['default']['#default_value'] = $value;
                      $form['#validate'][] = 'override_form_validate';//custom validation handler
                      break;
               }
      }
      
    • hook_form_validate function

      
      /**
      *Implement hook_form_validate()
      *Function to handle the OG reference
      */
      function override_form_validate($form, &$form_state) {
        $ids = array();
        $def_arys = override_og_ref_autocomplete_handler($ids, $form_state['input']['og_group_ref']['und'][0]['default'], 'default');
        $cmp_ary = override_og_ref_autocomplete_handler($def_arys, $form_state['input']['og_group_ref']['und'][0]['admin'], 'admin');
        $i = 0;
        foreach ($cmp_ary as $fieldmode => $value) {
            foreach ($value as $key => $ogid) {
              //assign the reference ID
                $form_state['values']['og_group_ref'][LANGUAGE_NONE][$i]['target_id'] = $ogid['nid'];
                $form_state['values']['og_group_ref'][LANGUAGE_NONE][$i++]['field_mode'] = $fieldmode;
            }
        }
      }
      /**
      *Implementing OG autocomplete handler
      */
      function override_og_ref_autocomplete_handler($ids, $og_ids, $type) {
        $og_ids_ary = explode(',', $og_ids);
        foreach ($og_ids_ary as $key => $value) {
            $target_id = db_select('node', 'n')
              ->fields('n', array('nid'))
              ->condition('title', trim($value), '=')
              ->condition('type', 'group', '=')
              ->execute()
              ->fetchAssoc();
            $ids[$type][$key] = $target_id;
        }
        return $ids;
      }
      
    • Copy og_entityreference_autocomplete_callback function from profiles\commons\modules\contrib\og\includes\og.field.inc and paste in your custom module and rename it to override_entityreference_autocomplete_callback

      
      /**
      *Menu callback: autocomplete the label of an entity.    
      *@param $type
      *The widget type (i.e. 'single' or 'tags').
      *@param $field_name
      *The name of the entity-reference field.
      *@param $entity_type
      *The entity type.
      *@param $bundle_name
      *The bundle name.
      *@param $field_mode
      *The field mode, "default" or "admin".
      *@param $entity_id
      *Optional; The entity ID the entity-reference field is attached to.
      *Defaults to ''.
      *@param $string
      *The label of the entity to query by.
      *@see entityreference_autocomplete_callback()
      */
      function override_entityreference_autocomplete_callback($type, $field_name, $entity_type, $bundle_name, $field_mode, $entity_id = '', $string = '') {
          $field = field_info_field($field_name);
          $instance = field_info_instance($entity_type, $field_name, $bundle_name);
          $instance = og_get_mocked_instance($instance, $field_mode);
              if (!$field || !$instance || $field['type'] != 'entityreference' || !field_access('edit', $field, $entity_type)) {
                return MENU_ACCESS_DENIED;
              }
              return override_entityreference_autocomplete_callback_get_matches($type, $field, $instance, $entity_type, $entity_id, $string);
      }
      
    • Also copy entityreference_autocomplete_callback_get_matches function from profiles\commons\modules\contrib\entityreference\entityreference.module and paste in your custom module and rename it to override_entityreference_autocomplete_callback_get_matches and remove ( entity_id ) suffix.

      
      /**
      *Return JSON based on given field, instance and string.
      *This function can be used by other modules that wish to pass a mocked
      *definition of the field on instance.
      *@param $type
      *The widget type (i.e. 'single' or 'tags').
      *@param $field
      *The field array defintion.
      *@param $instance
      *The instance array defintion.
      *@param $entity_type
      *The entity type.
      *@param $entity_id
      *Optional; The entity ID the entity-reference field is attached to.
      *Defaults to ''.
      *@param $string
      *The label of the entity to query by.
      */
      function override_entityreference_autocomplete_callback_get_matches($type, $field, $instance, $entity_type, $entity_id = '', $string = '') {
      $matches = array();
      $entity = NULL;
      if ($entity_id !== 'NULL') {
          $entity = entity_load_single($entity_type, $entity_id);
          $has_view_access = (entity_access('view', $entity_type, $entity) !== FALSE);
          $has_update_access = (entity_access('update', $entity_type, $entity) !== FALSE);
          if (!$entity || !($has_view_access || $has_update_access)) {
              return MENU_ACCESS_DENIED;
          }
      }
      $handler = entityreference_get_selection_handler($field, $instance, $entity_type, $entity);
      if ($type == 'tags') {
          // The user enters a comma-separated list of tags. We only autocomplete the last tag.
          $tags_typed = drupal_explode_tags($string);
          $tag_last = drupal_strtolower(array_pop($tags_typed));
          if (!empty($tag_last)) {
              $prefix = count($tags_typed) ? implode(', ', $tags_typed) . ', ' : '';
          }
      }
      else {
          // The user enters a single tag.
          $prefix = '';
          $tag_last = $string;
      }
      if (isset($tag_last)) {
          // Get an array of matching entities.
          $entity_labels = $handler->getReferencableEntities($tag_last, $instance['widget']['settings']['match_operator'], 10);
          // Loop through the products and convert them into autocomplete output.
          foreach ($entity_labels as $values) {
              foreach ($values as $entity_id => $label) {
                  $key = "$label"; //removed the entity id
                  // Strip things like starting/trailing white spaces, line breaks and tags.
                  $key = preg_replace('/\s\s+/', ' ', str_replace("\n", '', trim(decode_entities(strip_tags($key)))));
                  if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) {
                      $key = '"' . str_replace('"', '""', $key) . '"';
                  }
                  $matches[$prefix . $key] = '' . $label . '';
              }
          }
      }
      drupal_json_output($matches);
      }
      
      

Note: Add the additional conditions as per your requirements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.