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\field\FieldStorageConfigInterface;
20 require_once __DIR__ . '/field_test.entity.inc';
21 require_once __DIR__ . '/field_test.field.inc';
24 * Store and retrieve keyed data for later verification by unit tests.
26 * This function is a simple in-memory key-value store with the
27 * distinction that it stores all values for a given key instead of
28 * just the most recently set value. field_test module hooks call
29 * this function to record their arguments, keyed by hook name. The
30 * unit tests later call this function to verify that the correct
31 * hooks were called and were passed the correct arguments.
33 * This function ignores all calls until the first time it is called
34 * with $key of NULL. Each time it is called with $key of NULL, it
35 * erases all previously stored data from its internal cache, but also
36 * returns the previously stored data to the caller. A typical usage
40 * // calls to field_test_memorize() here are ignored
42 * // turn on memorization
43 * field_test_memorize();
45 * // call some Field API functions that invoke field_test hooks
46 * FieldStorageConfig::create($field_definition)->save();
48 * // retrieve and reset the memorized hook call data
49 * $mem = field_test_memorize();
51 * // make sure hook_field_storage_config_create() is invoked correctly
52 * assertEqual(count($mem['field_test_field_storage_config_create']), 1);
53 * assertEqual($mem['field_test_field_storage_config_create'][0], array($field));
57 * The key under which to store to $value, or NULL as described above.
59 * A value to store for $key.
61 * An array mapping each $key to an array of each $value passed in
64 function field_test_memorize($key = NULL, $value = NULL) {
65 $memorize = &drupal_static(__FUNCTION__, NULL);
72 if (is_array($memorize)) {
73 $memorize[$key][] = $value;
78 * Memorize calls to field_test_field_storage_config_create().
80 function field_test_field_storage_config_create(FieldStorageConfigInterface $field_storage) {
81 $args = func_get_args();
82 field_test_memorize(__FUNCTION__, $args);
86 * Implements hook_entity_display_build_alter().
88 function field_test_entity_display_build_alter(&$output, $context) {
89 $display_options = $context['display']->getComponent('test_field');
90 if (isset($display_options['settings']['alter'])) {
91 $output['test_field'][] = ['#markup' => 'field_test_entity_display_build_alter'];
94 if (isset($output['test_field'])) {
95 $output['test_field'][] = ['#markup' => 'entity language is ' . $context['entity']->language()->getId()];
100 * Implements hook_field_widget_form_alter().
102 function field_test_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
103 $field_definition = $context['items']->getFieldDefinition();
104 switch ($field_definition->getName()) {
105 case 'alter_test_text':
106 drupal_set_message('Field size: ' . $context['widget']->getSetting('size'));
109 case 'alter_test_options':
110 drupal_set_message('Widget type: ' . $context['widget']->getPluginId());
113 // Set a message if this is for the form displayed to set default value for
115 if ($context['default']) {
116 drupal_set_message('From hook_field_widget_form_alter(): Default form is true.');
121 * Implements hook_query_TAG_alter() for tag 'efq_table_prefixing_test'.
123 * @see \Drupal\system\Tests\Entity\EntityFieldQueryTest::testTablePrefixing()
125 function field_test_query_efq_table_prefixing_test_alter(&$query) {
126 // Add an additional join onto the entity base table. This will cause an
127 // exception if the EFQ does not properly prefix the base table.
128 $query->join('entity_test', 'et2', '%alias.id = entity_test.id');
133 * Implements hook_query_TAG_alter() for tag 'efq_metadata_test'.
135 * @see \Drupal\system\Tests\Entity\EntityQueryTest::testMetaData()
137 function field_test_query_efq_metadata_test_alter(&$query) {
138 global $efq_test_metadata;
139 $efq_test_metadata = $query->getMetadata('foo');
143 * Implements hook_entity_extra_field_info_alter().
145 function field_test_entity_extra_field_info_alter(&$info) {
146 // Remove all extra fields from the 'no_fields' content type;
147 unset($info['node']['no_fields']);
151 * Implements hook_entity_bundle_field_info_alter().
153 function field_test_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle) {
154 if (($field_name = \Drupal::state()->get('field_test_set_constraint', FALSE)) && $entity_type->id() == 'entity_test' && $bundle == 'entity_test' && !empty($fields[$field_name])) {
155 $fields[$field_name]->setPropertyConstraints('value', [
162 if (($field_name = \Drupal::state()->get('field_test_add_constraint', FALSE)) && $entity_type->id() == 'entity_test' && $bundle == 'entity_test' && !empty($fields[$field_name])) {
163 $fields[$field_name]->addPropertyConstraints('value', [