5 * Helper module for the Field API tests.
8 * - an entity type (field_test.entity.inc)
9 * - a field type and its formatters and widgets (field_test.field.inc)
10 * - a field storage backend (field_test.storage.inc)
12 * The main field_test.module file implements generic hooks and provides some
13 * 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;
21 require_once __DIR__ . '/field_test.entity.inc';
22 require_once __DIR__ . '/field_test.field.inc';
25 * Store and retrieve keyed data for later verification by unit tests.
27 * This function is a simple in-memory key-value store with the
28 * distinction that it stores all values for a given key instead of
29 * just the most recently set value. field_test module hooks call
30 * this function to record their arguments, keyed by hook name. The
31 * unit tests later call this function to verify that the correct
32 * hooks were called and were passed the correct arguments.
34 * This function ignores all calls until the first time it is called
35 * with $key of NULL. Each time it is called with $key of NULL, it
36 * erases all previously stored data from its internal cache, but also
37 * returns the previously stored data to the caller. A typical usage
41 * // calls to field_test_memorize() here are ignored
43 * // turn on memorization
44 * field_test_memorize();
46 * // call some Field API functions that invoke field_test hooks
47 * FieldStorageConfig::create($field_definition)->save();
49 * // retrieve and reset the memorized hook call data
50 * $mem = field_test_memorize();
52 * // make sure hook_field_storage_config_create() is invoked correctly
53 * assertEqual(count($mem['field_test_field_storage_config_create']), 1);
54 * assertEqual($mem['field_test_field_storage_config_create'][0], array($field));
58 * The key under which to store to $value, or NULL as described above.
60 * A value to store for $key.
62 * An array mapping each $key to an array of each $value passed in
65 function field_test_memorize($key = NULL, $value = NULL) {
66 $memorize = &drupal_static(__FUNCTION__, NULL);
73 if (is_array($memorize)) {
74 $memorize[$key][] = $value;
79 * Memorize calls to field_test_field_storage_config_create().
81 function field_test_field_storage_config_create(FieldStorageConfigInterface $field_storage) {
82 $args = func_get_args();
83 field_test_memorize(__FUNCTION__, $args);
87 * Implements hook_entity_display_build_alter().
89 function field_test_entity_display_build_alter(&$output, $context) {
90 $display_options = $context['display']->getComponent('test_field');
91 if (isset($display_options['settings']['alter'])) {
92 $output['test_field'][] = ['#markup' => 'field_test_entity_display_build_alter'];
95 if (isset($output['test_field'])) {
96 $output['test_field'][] = ['#markup' => 'entity language is ' . $context['entity']->language()->getId()];
101 * 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
106 if ($context['default']) {
108 ->addStatus('From hook_field_widget_form_alter(): Default form is true.');
113 * Implements hook_field_widget_multivalue_form_alter().
115 function field_test_field_widget_multivalue_form_alter(array &$elements, FormStateInterface $form_state, array $context) {
116 _field_test_alter_widget("hook_field_widget_multivalue_form_alter", $elements, $form_state, $context);
120 * Implements hook_field_widget_multivalue_WIDGET_TYPE_form_alter().
122 function field_test_field_widget_multivalue_test_field_widget_multiple_form_alter(array &$elements, FormStateInterface $form_state, array $context) {
123 _field_test_alter_widget("hook_field_widget_multivalue_WIDGET_TYPE_form_alter", $elements, $form_state, $context);
127 * Implements hook_field_widget_multivalue_WIDGET_TYPE_form_alter().
129 function field_test_field_widget_multivalue_test_field_widget_multiple_single_value_form_alter(array &$elements, FormStateInterface $form_state, array $context) {
130 _field_test_alter_widget("hook_field_widget_multivalue_WIDGET_TYPE_form_alter", $elements, $form_state, $context);
134 * Sets up alterations for widget alter tests.
136 * @see \Drupal\field\Tests\FormTest::widgetAlterTest()
138 function _field_test_alter_widget($hook, array &$elements, FormStateInterface $form_state, array $context) {
140 // Set a message if this is for the form displayed to set default value for
142 if ($context['default']) {
143 \Drupal::messenger()->addStatus("From $hook(): Default form is true.");
145 $alter_info = \Drupal::state()->get("field_test.widget_alter_test");
146 $name = $context['items']->getFieldDefinition()->getName();
147 if (!empty($alter_info) && $hook === $alter_info['hook'] && $name === $alter_info['field_name']) {
148 $elements['#prefix'] = "From $hook(): prefix on $name parent element.";
149 foreach (Element::children($elements) as $delta => $element) {
150 $elements[$delta]['#suffix'] = "From $hook(): suffix on $name child element.";
156 * Implements hook_query_TAG_alter() for tag 'efq_table_prefixing_test'.
158 * @see \Drupal\system\Tests\Entity\EntityFieldQueryTest::testTablePrefixing()
160 function field_test_query_efq_table_prefixing_test_alter(&$query) {
161 // Add an additional join onto the entity base table. This will cause an
162 // exception if the EFQ does not properly prefix the base table.
163 $query->join('entity_test', 'et2', '%alias.id = entity_test.id');
167 * Implements hook_query_TAG_alter() for tag 'efq_metadata_test'.
169 * @see \Drupal\system\Tests\Entity\EntityQueryTest::testMetaData()
171 function field_test_query_efq_metadata_test_alter(&$query) {
172 global $efq_test_metadata;
173 $efq_test_metadata = $query->getMetadata('foo');
177 * Implements hook_entity_extra_field_info_alter().
179 function field_test_entity_extra_field_info_alter(&$info) {
180 // Remove all extra fields from the 'no_fields' content type;
181 unset($info['node']['no_fields']);
185 * Implements hook_entity_bundle_field_info_alter().
187 function field_test_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle) {
188 if (($field_name = \Drupal::state()->get('field_test_constraint', FALSE)) && $entity_type->id() == 'entity_test' && $bundle == 'entity_test' && !empty($fields[$field_name])) {
189 // Set a property constraint using
190 // \Drupal\Core\Field\FieldConfigInterface::setPropertyConstraints().
191 $fields[$field_name]->setPropertyConstraints('value', [
194 'message' => t('%name does not accept the value @value.', ['%name' => $field_name, '@value' => -2]),
198 // Add a property constraint using
199 // \Drupal\Core\Field\FieldConfigInterface::addPropertyConstraints().
200 $fields[$field_name]->addPropertyConstraints('value', [
210 * Implements hook_field_ui_preconfigured_options_alter().
212 function field_test_field_ui_preconfigured_options_alter(array &$options, $field_type) {
213 if ($field_type === 'test_field_with_preconfigured_options') {
214 $options['custom_options']['entity_view_display']['settings'] = [
215 'test_formatter_setting_multiple' => 'altered dummy test string',