comparison core/modules/field/field.api.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 7a779792577d
children 129ea1e6d783
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
53 */ 53 */
54 function hook_field_info_alter(&$info) { 54 function hook_field_info_alter(&$info) {
55 // Change the default widget for fields of type 'foo'. 55 // Change the default widget for fields of type 'foo'.
56 if (isset($info['foo'])) { 56 if (isset($info['foo'])) {
57 $info['foo']['default widget'] = 'mymodule_widget'; 57 $info['foo']['default widget'] = 'mymodule_widget';
58 }
59 }
60
61 /**
62 * Perform alterations on preconfigured field options.
63 *
64 * @param array $options
65 * Array of options as returned from
66 * \Drupal\Core\Field\PreconfiguredFieldUiOptionsInterface::getPreconfiguredOptions().
67 * @param string $field_type
68 * The field type plugin ID.
69 *
70 * @see \Drupal\Core\Field\PreconfiguredFieldUiOptionsInterface::getPreconfiguredOptions()
71 */
72 function hook_field_ui_preconfigured_options_alter(array &$options, $field_type) {
73 // If the field is not an "entity_reference"-based field, bail out.
74 /** @var \Drupal\Core\Field\FieldTypePluginManager $field_type_manager */
75 $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
76 $class = $field_type_manager->getPluginClass($field_type);
77 if (!is_a($class, 'Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem', TRUE)) {
78 return;
79 }
80
81 // Set the default formatter for media in entity reference fields to be the
82 // "Rendered entity" formatter.
83 if (!empty($options['media'])) {
84 $options['media']['entity_view_display']['type'] = 'entity_reference_entity_view';
58 } 85 }
59 } 86 }
60 87
61 /** 88 /**
62 * Forbid a field storage update from occurring. 89 * Forbid a field storage update from occurring.
134 $info['options_select']['field_types'][] = 'my_field_type'; 161 $info['options_select']['field_types'][] = 'my_field_type';
135 } 162 }
136 163
137 /** 164 /**
138 * Alter forms for field widgets provided by other modules. 165 * Alter forms for field widgets provided by other modules.
166 *
167 * This hook can only modify individual elements within a field widget and
168 * cannot alter the top level (parent element) for multi-value fields. In most
169 * cases, you should use hook_field_widget_multivalue_form_alter() instead and
170 * loop over the elements.
139 * 171 *
140 * @param $element 172 * @param $element
141 * The field widget form element as constructed by 173 * The field widget form element as constructed by
142 * \Drupal\Core\Field\WidgetBaseInterface::form(). 174 * \Drupal\Core\Field\WidgetBaseInterface::form().
143 * @param $form_state 175 * @param $form_state
154 * form to set default values. 186 * form to set default values.
155 * 187 *
156 * @see \Drupal\Core\Field\WidgetBaseInterface::form() 188 * @see \Drupal\Core\Field\WidgetBaseInterface::form()
157 * @see \Drupal\Core\Field\WidgetBase::formSingleElement() 189 * @see \Drupal\Core\Field\WidgetBase::formSingleElement()
158 * @see hook_field_widget_WIDGET_TYPE_form_alter() 190 * @see hook_field_widget_WIDGET_TYPE_form_alter()
191 * @see hook_field_widget_multivalue_form_alter()
159 */ 192 */
160 function hook_field_widget_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context) { 193 function hook_field_widget_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context) {
161 // Add a css class to widget form elements for all fields of type mytype. 194 // Add a css class to widget form elements for all fields of type mytype.
162 $field_definition = $context['items']->getFieldDefinition(); 195 $field_definition = $context['items']->getFieldDefinition();
163 if ($field_definition->getType() == 'mytype') { 196 if ($field_definition->getType() == 'mytype') {
170 * Alter widget forms for a specific widget provided by another module. 203 * Alter widget forms for a specific widget provided by another module.
171 * 204 *
172 * Modules can implement hook_field_widget_WIDGET_TYPE_form_alter() to modify a 205 * Modules can implement hook_field_widget_WIDGET_TYPE_form_alter() to modify a
173 * specific widget form, rather than using hook_field_widget_form_alter() and 206 * specific widget form, rather than using hook_field_widget_form_alter() and
174 * checking the widget type. 207 * checking the widget type.
208 *
209 * This hook can only modify individual elements within a field widget and
210 * cannot alter the top level (parent element) for multi-value fields. In most
211 * cases, you should use hook_field_widget_multivalue_WIDGET_TYPE_form_alter()
212 * instead and loop over the elements.
175 * 213 *
176 * @param $element 214 * @param $element
177 * The field widget form element as constructed by 215 * The field widget form element as constructed by
178 * \Drupal\Core\Field\WidgetBaseInterface::form(). 216 * \Drupal\Core\Field\WidgetBaseInterface::form().
179 * @param $form_state 217 * @param $form_state
183 * and content of the array. 221 * and content of the array.
184 * 222 *
185 * @see \Drupal\Core\Field\WidgetBaseInterface::form() 223 * @see \Drupal\Core\Field\WidgetBaseInterface::form()
186 * @see \Drupal\Core\Field\WidgetBase::formSingleElement() 224 * @see \Drupal\Core\Field\WidgetBase::formSingleElement()
187 * @see hook_field_widget_form_alter() 225 * @see hook_field_widget_form_alter()
226 * @see hook_field_widget_multivalue_WIDGET_TYPE_form_alter()
188 */ 227 */
189 function hook_field_widget_WIDGET_TYPE_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context) { 228 function hook_field_widget_WIDGET_TYPE_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context) {
190 // Code here will only act on widgets of type WIDGET_TYPE. For example, 229 // Code here will only act on widgets of type WIDGET_TYPE. For example,
191 // hook_field_widget_mymodule_autocomplete_form_alter() will only act on 230 // hook_field_widget_mymodule_autocomplete_form_alter() will only act on
192 // widgets of type 'mymodule_autocomplete'. 231 // widgets of type 'mymodule_autocomplete'.
193 $element['#autocomplete_route_name'] = 'mymodule.autocomplete_route'; 232 $element['#autocomplete_route_name'] = 'mymodule.autocomplete_route';
233 }
234
235 /**
236 * Alter forms for multi-value field widgets provided by other modules.
237 *
238 * To alter the individual elements within the widget, loop over
239 * \Drupal\Core\Render\Element::children($elements).
240 *
241 * @param array $elements
242 * The field widget form elements as constructed by
243 * \Drupal\Core\Field\WidgetBase::formMultipleElements().
244 * @param \Drupal\Core\Form\FormStateInterface $form_state
245 * The current state of the form.
246 * @param array $context
247 * An associative array containing the following key-value pairs:
248 * - form: The form structure to which widgets are being attached. This may be
249 * a full form structure, or a sub-element of a larger form.
250 * - widget: The widget plugin instance.
251 * - items: The field values, as a
252 * \Drupal\Core\Field\FieldItemListInterface object.
253 * - default: A boolean indicating whether the form is being shown as a dummy
254 * form to set default values.
255 *
256 * @see \Drupal\Core\Field\WidgetBaseInterface::form()
257 * @see \Drupal\Core\Field\WidgetBase::formMultipleElements()
258 * @see hook_field_widget_multivalue_WIDGET_TYPE_form_alter()
259 */
260 function hook_field_widget_multivalue_form_alter(array &$elements, \Drupal\Core\Form\FormStateInterface $form_state, array $context) {
261 // Add a css class to widget form elements for all fields of type mytype.
262 $field_definition = $context['items']->getFieldDefinition();
263 if ($field_definition->getType() == 'mytype') {
264 // Be sure not to overwrite existing attributes.
265 $elements['#attributes']['class'][] = 'myclass';
266 }
267 }
268
269 /**
270 * Alter multi-value widget forms for a widget provided by another module.
271 *
272 * Modules can implement hook_field_widget_multivalue_WIDGET_TYPE_form_alter() to
273 * modify a specific widget form, rather than using
274 * hook_field_widget_form_alter() and checking the widget type.
275 *
276 * To alter the individual elements within the widget, loop over
277 * \Drupal\Core\Render\Element::children($elements).
278 *
279 * @param array $elements
280 * The field widget form elements as constructed by
281 * \Drupal\Core\Field\WidgetBase::formMultipleElements().
282 * @param \Drupal\Core\Form\FormStateInterface $form_state
283 * The current state of the form.
284 * @param array $context
285 * An associative array. See hook_field_widget_multivalue_form_alter() for
286 * the structure and content of the array.
287 *
288 * @see \Drupal\Core\Field\WidgetBaseInterface::form()
289 * @see \Drupal\Core\Field\WidgetBase::formMultipleElements()
290 * @see hook_field_widget_multivalue_form_alter()
291 */
292 function hook_field_widget_multivalue_WIDGET_TYPE_form_alter(array &$elements, \Drupal\Core\Form\FormStateInterface $form_state, array $context) {
293 // Code here will only act on widgets of type WIDGET_TYPE. For example,
294 // hook_field_widget_multivalue_mymodule_autocomplete_form_alter() will only
295 // act on widgets of type 'mymodule_autocomplete'.
296 // Change the autcomplete route for each autocomplete element within the
297 // multivalue widget.
298 foreach (Element::children($elements) as $delta => $element) {
299 $elements[$delta]['#autocomplete_route_name'] = 'mymodule.autocomplete_route';
300 }
194 } 301 }
195 302
196 /** 303 /**
197 * @} End of "defgroup field_widget". 304 * @} End of "defgroup field_widget".
198 */ 305 */