Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / field / tests / modules / field_test / field_test.module
1 <?php
2
3 /**
4  * @file
5  * Helper module for the Field API tests.
6  *
7  * The module defines
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)
11  *
12  * The main field_test.module file implements generic hooks and provides some
13  * test helper functions
14  */
15
16 use Drupal\Core\Entity\EntityTypeInterface;
17 use Drupal\Core\Form\FormStateInterface;
18 use Drupal\field\FieldStorageConfigInterface;
19
20 require_once __DIR__ . '/field_test.entity.inc';
21 require_once __DIR__ . '/field_test.field.inc';
22
23 /**
24  * Store and retrieve keyed data for later verification by unit tests.
25  *
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.
32  *
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
37  * scenario is:
38  *
39  * @code
40  *   // calls to field_test_memorize() here are ignored
41  *
42  *   // turn on memorization
43  *   field_test_memorize();
44  *
45  *   // call some Field API functions that invoke field_test hooks
46  *   FieldStorageConfig::create($field_definition)->save();
47  *
48  *   // retrieve and reset the memorized hook call data
49  *   $mem = field_test_memorize();
50  *
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));
54  * @endcode
55  *
56  * @param $key
57  *   The key under which to store to $value, or NULL as described above.
58  * @param $value
59  *   A value to store for $key.
60  * @return
61  *   An array mapping each $key to an array of each $value passed in
62  *   for that key.
63  */
64 function field_test_memorize($key = NULL, $value = NULL) {
65   $memorize = &drupal_static(__FUNCTION__, NULL);
66
67   if (!isset($key)) {
68     $return = $memorize;
69     $memorize = [];
70     return $return;
71   }
72   if (is_array($memorize)) {
73     $memorize[$key][] = $value;
74   }
75 }
76
77 /**
78  * Memorize calls to field_test_field_storage_config_create().
79  */
80 function field_test_field_storage_config_create(FieldStorageConfigInterface $field_storage) {
81   $args = func_get_args();
82   field_test_memorize(__FUNCTION__, $args);
83 }
84
85 /**
86  * Implements hook_entity_display_build_alter().
87  */
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'];
92   }
93
94   if (isset($output['test_field'])) {
95     $output['test_field'][] = ['#markup' => 'entity language is ' . $context['entity']->language()->getId()];
96   }
97 }
98
99 /**
100  * Implements hook_field_widget_form_alter().
101  */
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'));
107       break;
108
109     case 'alter_test_options':
110       drupal_set_message('Widget type: ' . $context['widget']->getPluginId());
111       break;
112   }
113   // Set a message if this is for the form displayed to set default value for
114   // the field.
115   if ($context['default']) {
116     drupal_set_message('From hook_field_widget_form_alter(): Default form is true.');
117   }
118 }
119
120 /**
121  * Implements hook_query_TAG_alter() for tag 'efq_table_prefixing_test'.
122  *
123  * @see \Drupal\system\Tests\Entity\EntityFieldQueryTest::testTablePrefixing()
124  */
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');
129 }
130
131
132 /**
133  * Implements hook_query_TAG_alter() for tag 'efq_metadata_test'.
134  *
135  * @see \Drupal\system\Tests\Entity\EntityQueryTest::testMetaData()
136  */
137 function field_test_query_efq_metadata_test_alter(&$query) {
138   global $efq_test_metadata;
139   $efq_test_metadata = $query->getMetadata('foo');
140 }
141
142 /**
143  * Implements hook_entity_extra_field_info_alter().
144  */
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']);
148 }
149
150 /**
151  * Implements hook_entity_bundle_field_info_alter().
152  */
153 function field_test_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle) {
154   if (($field_name = \Drupal::state()->get('field_test_constraint', FALSE)) && $entity_type->id() == 'entity_test' && $bundle == 'entity_test' && !empty($fields[$field_name])) {
155     // Set a property constraint using
156     // \Drupal\Core\Field\FieldConfigInterface::setPropertyConstraints().
157     $fields[$field_name]->setPropertyConstraints('value', [
158       'TestField' => [
159         'value' => -2,
160         'message' => t('%name does not accept the value @value.', ['%name' => $field_name, '@value' => -2]),
161       ],
162     ]);
163
164     // Add a property constraint using
165     // \Drupal\Core\Field\FieldConfigInterface::addPropertyConstraints().
166     $fields[$field_name]->addPropertyConstraints('value', [
167       'Range' => [
168         'min' => 0,
169         'max' => 32,
170       ],
171     ]);
172   }
173 }