Problem/Motivation

A Views selection handler can be set to Entity Reference fields without enabling Token module. But Token module is necessary for the handler to work.

When the handler is used without enabling Token module, the following warning occurs.

Undefined index: token type in EntityReference_SelectionHandler_Views->handleArgs() 

The steps to reproduce is the following:

  1. Disable Token module if already installed.
  2. Install Views, Views UI, Field UI and Entity Reference modules.
  3. Create an Entity Reference view.
  4. Create a Entity Reference field and select "Views: Filter by an entity reference view" for "Entity Selection Mode" and select the view created in the previous step for "View used to select the entities".
  5. Create a node of the node type with the Entity Reference field and save it.
  6. The warning is logged.

The cause is the following code.

EntityReference_SelectionHandler_Views.class.php:


  /**
   * Handles arguments for views.
   *
   * Replaces tokens using token_replace().
   *
   * @param array $args
   *   Usually $this->field['settings']['handler_settings']['view']['args'].
   *
   * @return array
   *   The arguments to be send to the View.
   */
  protected function handleArgs($args) {
    // Parameters for token_replace().
    $data = array();
    $options = array('clear' => TRUE);

    if ($entity = $this->entity) {
      // D7 HACK: For new entities, entity and revision id are not set. This leads to
      // * token replacement emitting PHP warnings
      // * views choking on empty arguments
      // We workaround this by filling in '0' for these IDs
      // and use a clone to leave no traces of our unholy doings.
      $info = entity_get_info($this->instance['entity_type']);
      if (!isset($entity->{$info['entity keys']['id']})) {
        $entity = clone $entity;
        $entity->{$info['entity keys']['id']} = '0';
        if (!empty($info['entity keys']['revision'])) {
          $entity->{$info['entity keys']['revision']} = '0';
        }
      }

      $data[$info['token type']] = $entity;
    }
    // Replace tokens for each argument.
    foreach ($args as $key => $arg) {
      $args[$key] = token_replace($arg, $data, $options);
    }
    return $args;
  }
}

This part seems to have been introduced in #2010898: Use tokens for entity selection view arguments.

Proposed resolution

Adding a check for the status of Token module.

Remaining tasks

Clarify the steps to reproduce.
Create a patch.
Review the patch.

User interface changes

(None)

API changes

(None)

Data model changes

(None)

Comments

hgoto created an issue. See original summary.

hgoto’s picture

Here is a patch. This adds a simple check.

hgoto’s picture

Status: Active » Needs review

  • minorOffense committed 00aef8f on 7.x-1.x authored by hgoto
    Issue #2857322 by hgoto: Functions of Token module is used without...
minoroffense’s picture

Status: Needs review » Fixed

Thanks for the fix!

hgoto’s picture

@minorOffense, thank you!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.