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:
- Disable Token module if already installed.
- Install Views, Views UI, Field UI and Entity Reference modules.
- Create an Entity Reference view.
- 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".
- Create a node of the node type with the Entity Reference field and save it.
- 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)
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | entityreference-check_token_module-2857322-2.patch | 690 bytes | hgoto |
Comments
Comment #2
hgoto commentedHere is a patch. This adds a simple check.
Comment #3
hgoto commentedComment #5
minoroffense commentedThanks for the fix!
Comment #6
hgoto commented@minorOffense, thank you!