Commit 28a17bc5 authored by webchick's avatar webchick

#990148 by chx, jhodgdon: Provide context to hook_field_attach_view_alter().

parent 731bc4fd
......@@ -1277,7 +1277,7 @@ function hook_field_attach_purge($entity_type, $entity, $field, $instance) {
}
/**
* Perform alterations on field_attach_view().
* Perform alterations on field_attach_view() or field_view_field().
*
* This hook is invoked after the field module has performed the operation.
*
......@@ -1287,7 +1287,13 @@ function hook_field_attach_purge($entity_type, $entity, $field, $instance) {
* An associative array containing:
* - entity_type: The type of $entity; for example, 'node' or 'user'.
* - entity: The entity with fields to render.
* - view_mode: View mode, for example, 'full' or 'teaser'.
* - view_mode: View mode; for example, 'full' or 'teaser'.
* - display: Either a view mode string or an array of display settings. If
* this hook is being invoked from field_attach_view(), the 'display'
* element is set to the view mode string. If this hook is being invoked
* from field_view_field(), this element is set to the $display argument
* and the view_mode element is set to '_custom'. See field_view_field()
* for more information on what its $display argument contains.
* - language: The language code used for rendering.
*/
function hook_field_attach_view_alter(&$output, $context) {
......
......@@ -1166,6 +1166,7 @@ function field_attach_view($entity_type, $entity, $view_mode, $langcode = NULL)
'entity_type' => $entity_type,
'entity' => $entity,
'view_mode' => $view_mode,
'display' => $view_mode,
'language' => $langcode,
);
drupal_alter('field_attach_view', $output, $context);
......
......@@ -705,6 +705,7 @@ function field_view_field($entity_type, $entity, $field_name, $display = array()
'entity_type' => $entity_type,
'entity' => $entity,
'view_mode' => '_custom',
'display' => $display,
);
drupal_alter('field_attach_view', $result, $context);
......
......@@ -1743,12 +1743,14 @@ class FieldDisplayAPITestCase extends FieldTestCase {
'type' => 'field_test_multiple',
'settings' => array(
'test_formatter_setting_multiple' => $this->randomName(),
'alter' => TRUE,
),
);
$output = field_view_field('test_entity', $this->entity, $this->field_name, $display);
$this->drupalSetContent(drupal_render($output));
$setting = $display['settings']['test_formatter_setting_multiple'];
$this->assertNoText($this->label, t('Label was not displayed.'));
$this->assertText('field_test_field_attach_view_alter', t('Alter fired, display passed.'));
$array = array();
foreach ($this->values as $delta => $value) {
$array[] = $delta . ':' . $value['value'];
......@@ -1768,6 +1770,7 @@ class FieldDisplayAPITestCase extends FieldTestCase {
$this->drupalSetContent($view);
$setting = $display['settings']['test_formatter_setting_additional'];
$this->assertNoText($this->label, t('Label was not displayed.'));
$this->assertNoText('field_test_field_attach_view_alter', t('Alter not fired.'));
foreach ($this->values as $delta => $value) {
$this->assertText($setting . '|' . $value['value'] . '|' . ($value['value'] + 1), t('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
}
......
......@@ -235,3 +235,12 @@ function field_test_dummy_field_storage_query(EntityFieldQuery $query) {
function field_test_entity_label_callback($entity) {
return 'label callback ' . $entity->ftlabel;
}
/**
* Implements hook_field_attach_view_alter().
*/
function field_test_field_attach_view_alter(&$output, $context) {
if (!empty($context['display']['settings']['alter'])) {
$output['test_field'][] = array('#markup' => 'field_test_field_attach_view_alter');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment