field_test.module 7.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php

/**
 * @file
 * Helper module for the Field API tests.
 *
 * The module defines
 * - an entity type (field_test.entity.inc)
 * - a field type and its formatters and widgets (field_test.field.inc)
 * - a field storage backend (field_test.storage.inc)
 *
 * The main field_test.module file implements generic hooks and provides some
 * test helper functions
 */

16
use Drupal\Core\Entity\EntityTypeInterface;
17
use Drupal\Core\Form\FormStateInterface;
18
use Drupal\Core\Render\Element;
19
use Drupal\field\FieldStorageConfigInterface;
20

21 22
require_once __DIR__ . '/field_test.entity.inc';
require_once __DIR__ . '/field_test.field.inc';
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

/**
 * Store and retrieve keyed data for later verification by unit tests.
 *
 * This function is a simple in-memory key-value store with the
 * distinction that it stores all values for a given key instead of
 * just the most recently set value. field_test module hooks call
 * this function to record their arguments, keyed by hook name. The
 * unit tests later call this function to verify that the correct
 * hooks were called and were passed the correct arguments.
 *
 * This function ignores all calls until the first time it is called
 * with $key of NULL. Each time it is called with $key of NULL, it
 * erases all previously stored data from its internal cache, but also
 * returns the previously stored data to the caller. A typical usage
 * scenario is:
 *
 * @code
 *   // calls to field_test_memorize() here are ignored
 *
 *   // turn on memorization
 *   field_test_memorize();
 *
 *   // call some Field API functions that invoke field_test hooks
47
 *   FieldStorageConfig::create($field_definition)->save();
48 49 50 51
 *
 *   // retrieve and reset the memorized hook call data
 *   $mem = field_test_memorize();
 *
52 53 54
 *   // make sure hook_field_storage_config_create() is invoked correctly
 *   assertEqual(count($mem['field_test_field_storage_config_create']), 1);
 *   assertEqual($mem['field_test_field_storage_config_create'][0], array($field));
55 56 57 58 59 60 61 62 63 64 65 66 67
 * @endcode
 *
 * @param $key
 *   The key under which to store to $value, or NULL as described above.
 * @param $value
 *   A value to store for $key.
 * @return
 *   An array mapping each $key to an array of each $value passed in
 *   for that key.
 */
function field_test_memorize($key = NULL, $value = NULL) {
  $memorize = &drupal_static(__FUNCTION__, NULL);

68
  if (!isset($key)) {
69
    $return = $memorize;
70
    $memorize = [];
71 72 73 74 75 76 77 78
    return $return;
  }
  if (is_array($memorize)) {
    $memorize[$key][] = $value;
  }
}

/**
79
 * Memorize calls to field_test_field_storage_config_create().
80
 */
81
function field_test_field_storage_config_create(FieldStorageConfigInterface $field_storage) {
82 83 84 85
  $args = func_get_args();
  field_test_memorize(__FUNCTION__, $args);
}

86
/**
87
 * Implements hook_entity_display_build_alter().
88
 */
89 90 91
function field_test_entity_display_build_alter(&$output, $context) {
  $display_options = $context['display']->getComponent('test_field');
  if (isset($display_options['settings']['alter'])) {
92
    $output['test_field'][] = ['#markup' => 'field_test_entity_display_build_alter'];
93
  }
94 95

  if (isset($output['test_field'])) {
96
    $output['test_field'][] = ['#markup' => 'entity language is ' . $context['entity']->language()->getId()];
97
  }
98
}
99 100 101 102

/**
 * Implements hook_field_widget_form_alter().
 */
103
function field_test_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
104
  // Set a message if this is for the form displayed to set default value for
105
  // the field.
106 107 108
  if ($context['default']) {
    drupal_set_message('From hook_field_widget_form_alter(): Default form is true.');
  }
109
}
110

111 112 113 114 115 116 117 118
/**
 * Implements hook_field_widget_multivalue_form_alter().
 */
function field_test_field_widget_multivalue_form_alter(array &$elements, FormStateInterface $form_state, array $context) {
  _field_test_alter_widget("hook_field_widget_multivalue_form_alter", $elements, $form_state, $context);
}

/**
119
 * Implements hook_field_widget_multivalue_WIDGET_TYPE_form_alter().
120
 */
121
function field_test_field_widget_multivalue_test_field_widget_multiple_form_alter(array &$elements, FormStateInterface $form_state, array $context) {
122 123 124
  _field_test_alter_widget("hook_field_widget_multivalue_WIDGET_TYPE_form_alter", $elements, $form_state, $context);
}

125 126 127 128 129 130 131 132
/**
 * Implements hook_field_widget_multivalue_WIDGET_TYPE_form_alter().
 */
function field_test_field_widget_multivalue_test_field_widget_multiple_single_value_form_alter(array &$elements, FormStateInterface $form_state, array $context) {
  _field_test_alter_widget("hook_field_widget_multivalue_WIDGET_TYPE_form_alter", $elements, $form_state, $context);
}


133 134 135 136 137 138 139 140 141 142 143 144
/**
 * Sets up alterations for widget alter tests.
 *
 * @see \Drupal\field\Tests\FormTest::widgetAlterTest()
 */
function _field_test_alter_widget($hook, array &$elements, FormStateInterface $form_state, array $context) {

  // Set a message if this is for the form displayed to set default value for
  // the field.
  if ($context['default']) {
    drupal_set_message("From $hook(): Default form is true.");
  }
145 146 147 148 149 150
  $alter_info = \Drupal::state()->get("field_test.widget_alter_test");
  $name = $context['items']->getFieldDefinition()->getName();
  if (!empty($alter_info) && $hook === $alter_info['hook'] && $name === $alter_info['field_name']) {
    $elements['#prefix'] = "From $hook(): prefix on $name parent element.";
    foreach (Element::children($elements) as $delta => $element) {
      $elements[$delta]['#suffix'] = "From $hook(): suffix on $name child element.";
151 152 153 154
    }
  }
}

155 156 157
/**
 * Implements hook_query_TAG_alter() for tag 'efq_table_prefixing_test'.
 *
158
 * @see \Drupal\system\Tests\Entity\EntityFieldQueryTest::testTablePrefixing()
159 160 161 162
 */
function field_test_query_efq_table_prefixing_test_alter(&$query) {
  // Add an additional join onto the entity base table. This will cause an
  // exception if the EFQ does not properly prefix the base table.
163
  $query->join('entity_test', 'et2', '%alias.id = entity_test.id');
164
}
165

166 167 168 169

/**
 * Implements hook_query_TAG_alter() for tag 'efq_metadata_test'.
 *
170
 * @see \Drupal\system\Tests\Entity\EntityQueryTest::testMetaData()
171 172 173 174 175 176
 */
function field_test_query_efq_metadata_test_alter(&$query) {
  global $efq_test_metadata;
  $efq_test_metadata = $query->getMetadata('foo');
}

177
/**
178
 * Implements hook_entity_extra_field_info_alter().
179
 */
180
function field_test_entity_extra_field_info_alter(&$info) {
181 182 183
  // Remove all extra fields from the 'no_fields' content type;
  unset($info['node']['no_fields']);
}
184 185 186 187

/**
 * Implements hook_entity_bundle_field_info_alter().
 */
188
function field_test_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle) {
189 190 191
  if (($field_name = \Drupal::state()->get('field_test_constraint', FALSE)) && $entity_type->id() == 'entity_test' && $bundle == 'entity_test' && !empty($fields[$field_name])) {
    // Set a property constraint using
    // \Drupal\Core\Field\FieldConfigInterface::setPropertyConstraints().
192
    $fields[$field_name]->setPropertyConstraints('value', [
193 194 195
      'TestField' => [
        'value' => -2,
        'message' => t('%name does not accept the value @value.', ['%name' => $field_name, '@value' => -2]),
196 197
      ],
    ]);
198 199 200

    // Add a property constraint using
    // \Drupal\Core\Field\FieldConfigInterface::addPropertyConstraints().
201 202 203 204 205 206 207 208
    $fields[$field_name]->addPropertyConstraints('value', [
      'Range' => [
        'min' => 0,
        'max' => 32,
      ],
    ]);
  }
}
209 210 211 212 213 214 215 216 217 218 219

/**
 * Implements hook_field_ui_preconfigured_options_alter().
 */
function field_test_field_ui_preconfigured_options_alter(array &$options, $field_type) {
  if ($field_type === 'test_field_with_preconfigured_options') {
    $options['custom_options']['entity_view_display']['settings'] = [
      'test_formatter_setting_multiple' => 'altered dummy test string',
    ];
  }
}