class FieldCollectionEmbedWidget in Field collection 8
Same name and namespace in other branches
- 8.3 src/Plugin/Field/FieldWidget/FieldCollectionEmbedWidget.php \Drupal\field_collection\Plugin\Field\FieldWidget\FieldCollectionEmbedWidget
Plugin implementation of the 'field_collection_embed' widget.
Plugin annotation
@FieldWidget(
id = "field_collection_embed",
label = @Translation("Embedded"),
field_types = {
"field_collection"
},
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Field\PluginSettingsBase implements DependentPluginInterface, PluginSettingsInterface
- class \Drupal\Core\Field\WidgetBase implements WidgetInterface, ContainerFactoryPluginInterface uses AllowedTagsXssTrait
- class \Drupal\field_collection\Plugin\Field\FieldWidget\FieldCollectionEmbedWidget
- class \Drupal\Core\Field\WidgetBase implements WidgetInterface, ContainerFactoryPluginInterface uses AllowedTagsXssTrait
- class \Drupal\Core\Field\PluginSettingsBase implements DependentPluginInterface, PluginSettingsInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of FieldCollectionEmbedWidget
File
- src/
Plugin/ Field/ FieldWidget/ FieldCollectionEmbedWidget.php, line 26
Namespace
Drupal\field_collection\Plugin\Field\FieldWidgetView source
class FieldCollectionEmbedWidget extends WidgetBase {
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
// TODO: Detect recursion
$field_name = $this->fieldDefinition
->getName();
// Nest the field collection item entity form in a dedicated parent space,
// by appending [field_name, delta] to the current parent space.
// That way the form values of the field collection item are separated.
$parents = array_merge($element['#field_parents'], [
$field_name,
$delta,
]);
$element += [
'#element_validate' => [
[
static::class,
'validate',
],
],
'#parents' => $parents,
'#field_name' => $field_name,
];
if ($this->fieldDefinition
->getFieldStorageDefinition()
->getCardinality() == 1) {
$element['#type'] = 'fieldset';
}
$field_state = static::getWidgetState($element['#field_parents'], $field_name, $form_state);
if (isset($field_state['field_collection_item'][$delta])) {
$field_collection_item = $field_state['field_collection_item'][$delta];
}
else {
$field_collection_item = $items[$delta]
->getFieldCollectionItem(TRUE);
// Put our entity in the form state, so FAPI callbacks can access it.
$field_state['field_collection_item'][$delta] = $field_collection_item;
}
static::setWidgetState($element['#field_parents'], $field_name, $form_state, $field_state);
$display = entity_get_form_display('field_collection_item', $field_name, 'default');
$display
->buildForm($field_collection_item, $element, $form_state);
if (empty($element['#required'])) {
$element['#after_build'][] = [
static::class,
'delayRequiredValidation',
];
// Stop HTML5 form validation so our validation code can run instead.
$form['#attributes']['novalidate'] = 'novalidate';
}
// Put the remove button on unlimited cardinality field collection fields.
if ($this->fieldDefinition
->getFieldStorageDefinition()
->getCardinality() == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
$options = [
'query' => [
'element_parents' => implode('/', $element['#parents']),
],
];
$element['actions'] = [
'#type' => 'actions',
'remove_button' => [
'#delta' => $delta,
'#name' => implode('_', $parents) . '_remove_button',
'#type' => 'submit',
'#value' => t('Remove'),
'#validate' => [],
'#submit' => [
[
static::class,
'removeSubmit',
],
],
'#limit_validation_errors' => [],
'#ajax' => [
'callback' => [
$this,
'ajaxRemove',
],
'options' => $options,
'effect' => 'fade',
'wrapper' => $form['#wrapper_id'],
],
'#weight' => 1000,
],
];
}
return $element;
}
/**
* {@inheritdoc}
*/
protected function formMultipleElements(FieldItemListInterface $items, array &$form, FormStateInterface $form_state) {
// We don't want to render empty items on field collection fields
// unless a) the field collection is empty ; b) the form is rebuilding,
// which means that the user clicked on "Add another item"; or
// c) we are creating a new entity.
if (count($items) > 0 && !$form_state
->isRebuilding() && !$items
->getEntity()
->isNew()) {
$field_name = $this->fieldDefinition
->getName();
$cardinality = $this->fieldDefinition
->getFieldStorageDefinition()
->getCardinality();
$parents = $form['#parents'];
if ($cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
$field_state = static::getWidgetState($parents, $field_name, $form_state);
$field_state['items_count']--;
static::setWidgetState($parents, $field_name, $form_state, $field_state);
}
}
// Adjust wrapper identifiers as they are shared between parents and
// children in nested field collections.
$form['#wrapper_id'] = Html::getUniqueID($items
->getName());
$elements = parent::formMultipleElements($items, $form, $form_state);
$elements['#prefix'] = '<div id="' . $form['#wrapper_id'] . '">';
$elements['#suffix'] = '</div>';
$elements['add_more']['#ajax']['wrapper'] = $form['#wrapper_id'];
return $elements;
}
/**
* #after_build of a field collection element.
*
* Delays the validation of #required.
*/
public static function delayRequiredValidation($element, FormStateInterface $form_state) {
// If the process_input flag is set, the form and its input is going to be
// validated. Prevent #required (sub)fields from throwing errors while
// their non-#required field collection item is empty.
if ($form_state
->isProcessingInput()) {
static::collectRequiredElements($element, $element['#field_collection_required_elements']);
}
return $element;
}
/**
* Prevent the default 'required' validation from running on subfields.
*/
private static function collectRequiredElements(&$element, &$required_elements) {
// Recurse through all children.
foreach (Element::children($element) as $key) {
if (isset($element[$key]) && $element[$key]) {
static::collectRequiredElements($element[$key], $required_elements);
}
}
if (!empty($element['#required'])) {
$element['#required'] = FALSE;
$required_elements[] =& $element;
$element += [
'#pre_render' => [],
];
array_unshift($element['#pre_render'], [
static::class,
'renderRequired',
]);
}
}
/**
* #pre_render callback ensures the element is rendered as being required.
*/
public static function renderRequired($element) {
$element['#required'] = TRUE;
return $element;
}
/**
* FAPI validation of an individual field collection element.
*/
public static function validate($element, FormStateInterface $form_state, $form) {
$field_parents = $element['#field_parents'];
$field_name = $element['#field_name'];
$field_state = static::getWidgetState($field_parents, $field_name, $form_state);
$field_collection_item = $field_state['field_collection_item'][$element['#delta']];
$display = entity_get_form_display('field_collection_item', $field_name, 'default');
$display
->extractFormValues($field_collection_item, $element, $form_state);
// Now validate required elements if the entity is not empty.
if (!$field_collection_item
->isEmpty() && !empty($element['#field_collection_required_elements'])) {
foreach ($element['#field_collection_required_elements'] as &$elements) {
// Copied from \Drupal\Core\Form\FormValidator::doValidateForm().
// #1676206: Modified to support options widget.
if (isset($elements['#needs_validation'])) {
$is_countable = is_array($elements['#value']) || $elements['#value'] instanceof \Countable;
$is_empty_multiple = $is_countable && count($elements['#value']) == 0;
// @todo: replace Unicode::strlen() with mb_substr() per
// https://www.drupal.org/project/drupal/issues/2849669
$is_empty_string = is_string($elements['#value']) && Unicode::strlen(trim($elements['#value'])) == 0;
$is_empty_value = $elements['#value'] === 0;
$is_empty_option = isset($elements['#options']['_none']) && $elements['#value'] == '_none';
if ($is_empty_multiple || $is_empty_string || $is_empty_value || $is_empty_option) {
if (isset($elements['#required_error'])) {
$form_state
->setError($elements, $elements['#required_error']);
}
else {
if (isset($elements['#title'])) {
$form_state
->setError($elements, t('@name field is required.', [
'@name' => $elements['#title'],
]));
}
else {
$form_state
->setError($elements);
}
}
}
}
}
}
// Only if the form is being submitted, finish the collection entity and
// prepare it for saving.
if ($form_state
->isSubmitted() && !$form_state
->hasAnyErrors()) {
// Load initial form values into $item, so any other form values below the
// same parents are kept.
$field = NestedArray::getValue($form_state
->getValues(), $element['#parents']);
// Set the _weight if it is a multiple field.
$element_widget = NestedArray::getValue($form, array_slice($element['#array_parents'], 0, -1));
if (isset($element['_weight']) && $element_widget['#cardinality_multiple']) {
$field['_weight'] = $element['_weight']['#value'];
}
// Put the field collection field in $field['field_collection_item'], so
// it is saved with the host entity via FieldCollection->preSave() / field
// API if it is not empty.
$field['field_collection_item'] = $field_collection_item;
$form_state
->setValue($element['#parents'], $field);
}
}
/**
* Submit callback to remove an item from the field UI multiple wrapper.
*
* When a remove button is submitted, we need to find the item that it
* referenced and delete it. Since field UI has the deltas as a straight
* unbroken array key, we have to renumber everything down. Since we do this
* we *also* need to move all the deltas around in the $form_state->values
* and $form_state input so that user changed values follow. This is a bit
* of a complicated process.
*/
public static function removeSubmit($form, FormStateInterface $form_state) {
$button = $form_state
->getTriggeringElement();
$delta = $button['#delta'];
// Where in the form we'll find the parent element.
$address = array_slice($button['#array_parents'], 0, -4);
$address_state = array_slice($button['#parents'], 0, -3);
// Go one level up in the form, to the widgets container.
$parent_element = NestedArray::getValue($form, array_merge($address, [
'widget',
]));
$field_name = $parent_element['#field_name'];
$parents = $parent_element['#field_parents'];
$field_state = static::getWidgetState($parents, $field_name, $form_state);
// Go ahead and renumber everything from our delta to the last
// item down one. This will overwrite the item being removed.
for ($i = $delta; $i <= $field_state['items_count']; $i++) {
$old_element_address = array_merge($address, [
'widget',
$i + 1,
]);
$old_element_state_address = array_merge($address_state, [
$i + 1,
]);
$new_element_state_address = array_merge($address_state, [
$i,
]);
$moving_element = NestedArray::getValue($form, $old_element_address);
$moving_element_value = NestedArray::getValue($form_state
->getValues(), $old_element_state_address);
$moving_element_input = NestedArray::getValue($form_state
->getUserInput(), $old_element_state_address);
// Tell the element where it's being moved to.
$moving_element['#parents'] = $new_element_state_address;
// Move the element around.
$form_state
->setValueForElement($moving_element, $moving_element_value);
$user_input = $form_state
->getUserInput();
NestedArray::setValue($user_input, $moving_element['#parents'], $moving_element_input);
$form_state
->setUserInput($user_input);
// Move the entity in our saved state.
if (isset($field_state['field_collection_item'][$i + 1])) {
$field_state['field_collection_item'][$i] = $field_state['field_collection_item'][$i + 1];
}
else {
unset($field_state['field_collection_item'][$i]);
}
}
// Then remove the last item. But we must not go negative.
if ($field_state['items_count'] > 0) {
$field_state['items_count']--;
}
else {
// Create a new field collection item after deleting the last one so the
// form will show a blank field collection item instead of resurrecting
// the first one if there was already data.
$field_state['field_collection_item'][0] = FieldCollectionItem::create([
'field_name' => $field_name,
]);
}
// Fix the weights. Field UI lets the weights be in a range of
// (-1 * item_count) to (item_count). This means that when we remove one,
// the range shrinks; weights outside of that range then get set to
// the first item in the select by the browser, floating them to the top.
// We use a brute force method because we lost weights on both ends
// and if the user has moved things around, we have to cascade because
// if I have items weight weights 3 and 4, and I change 4 to 3 but leave
// the 3, the order of the two 3s now is undefined and may not match what
// the user had selected.
$input = NestedArray::getValue($form_state
->getUserInput(), $address_state);
// Sort by weight.
uasort($input, '_field_collection_sort_items_helper');
// Reweight everything in the correct order.
$weight = -1 * $field_state['items_count'];
foreach ($input as $key => $item) {
if ($item) {
$input[$key]['_weight'] = $weight++;
}
}
$user_input = $form_state
->getUserInput();
NestedArray::setValue($user_input, $address_state, $input);
$form_state
->setUserInput($user_input);
static::setWidgetState($parents, $field_name, $form_state, $field_state);
$form_state
->setRebuild();
}
/**
* Ajax callback to remove a field collection from a multi-valued field.
*
* @param array $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
*
* @return \Drupal\Core\Ajax\AjaxResponse
* An AjaxResponse object.
*
* @see self::removeSubmit()
*/
function ajaxRemove(array $form, FormStateInterface &$form_state) {
// At this point, $this->removeSubmit() removed the element so we just need
// to return the parent element.
$button = $form_state
->getTriggeringElement();
return NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -3));
}
/**
* Submission handler for the "Add another item" button.
*/
public static function addMoreSubmit(array $form, FormStateInterface $form_state) {
$button = $form_state
->getTriggeringElement();
// Go one level up in the form, to the widgets container.
$element = NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -1));
$field_name = $element['#field_name'];
$parents = $element['#field_parents'];
// Increment the items count.
$field_state = static::getWidgetState($parents, $field_name, $form_state);
$field_state['items_count']++;
// Make a new field collection item. This helps to ensure that
// trying to add a field collection item won't ressurect a deleted one from
// the trash bin.
$field_state['field_collection_item'][$field_state['items_count']] = FieldCollectionItem::create([
'field_name' => $field_name,
]);
static::setWidgetState($parents, $field_name, $form_state, $field_state);
$form_state
->setRebuild();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AllowedTagsXssTrait:: |
public | function | Returns a list of tags allowed by AllowedTagsXssTrait::fieldFilterXss(). | |
AllowedTagsXssTrait:: |
public | function | Returns a human-readable list of allowed tags for display in help texts. | |
AllowedTagsXssTrait:: |
public | function | Filters an HTML string to prevent XSS vulnerabilities. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FieldCollectionEmbedWidget:: |
public static | function |
Submission handler for the "Add another item" button. Overrides WidgetBase:: |
|
FieldCollectionEmbedWidget:: |
function | Ajax callback to remove a field collection from a multi-valued field. | ||
FieldCollectionEmbedWidget:: |
private static | function | Prevent the default 'required' validation from running on subfields. | |
FieldCollectionEmbedWidget:: |
public static | function | #after_build of a field collection element. | |
FieldCollectionEmbedWidget:: |
public | function |
Returns the form for a single field widget. Overrides WidgetInterface:: |
|
FieldCollectionEmbedWidget:: |
protected | function |
Special handling to create form elements for multiple values. Overrides WidgetBase:: |
|
FieldCollectionEmbedWidget:: |
public static | function | Submit callback to remove an item from the field UI multiple wrapper. | |
FieldCollectionEmbedWidget:: |
public static | function | #pre_render callback ensures the element is rendered as being required. | |
FieldCollectionEmbedWidget:: |
public static | function | FAPI validation of an individual field collection element. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginSettingsBase:: |
protected | property | Whether default settings have been merged into the current $settings. | |
PluginSettingsBase:: |
protected | property | The plugin settings injected by third party modules. | |
PluginSettingsBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
6 |
PluginSettingsBase:: |
public static | function |
Defines the default settings for this plugin. Overrides PluginSettingsInterface:: |
42 |
PluginSettingsBase:: |
public | function |
Returns the value of a setting, or its default value if absent. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Returns the array of settings, including defaults for missing settings. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Gets the list of third parties that store information. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Gets the value of a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Gets all third-party settings of a given module. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
protected | function | Merges default settings values into $settings. | |
PluginSettingsBase:: |
public | function |
Informs the plugin that some configuration it depends on will be deleted. Overrides PluginSettingsInterface:: |
3 |
PluginSettingsBase:: |
public | function |
Sets the value of a setting for the plugin. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Sets the settings for the plugin. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Sets the value of a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Unsets a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
WidgetBase:: |
protected | property | The field definition. | |
WidgetBase:: |
protected | property |
The widget settings. Overrides PluginSettingsBase:: |
|
WidgetBase:: |
public static | function | Ajax callback for the "Add another item" button. | |
WidgetBase:: |
public static | function | After-build handler for field elements in a form. | |
WidgetBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
5 |
WidgetBase:: |
public | function |
Assigns a field-level validation error to the right widget sub-element. Overrides WidgetInterface:: |
8 |
WidgetBase:: |
public | function |
Extracts field values from submitted form values. Overrides WidgetBaseInterface:: |
2 |
WidgetBase:: |
public | function |
Reports field-level validation errors against actual form elements. Overrides WidgetBaseInterface:: |
2 |
WidgetBase:: |
public | function |
Creates a form element for a field. Overrides WidgetBaseInterface:: |
3 |
WidgetBase:: |
protected | function | Generates the form element for a single copy of the widget. | |
WidgetBase:: |
protected | function | Returns the value of a field setting. | |
WidgetBase:: |
protected | function | Returns the array of field settings. | |
WidgetBase:: |
protected | function | Returns the filtered field description. | |
WidgetBase:: |
public static | function |
Retrieves processing information about the widget from $form_state. Overrides WidgetBaseInterface:: |
|
WidgetBase:: |
protected static | function | Returns the location of processing information within $form_state. | |
WidgetBase:: |
protected | function | Returns whether the widget handles multiple values. | |
WidgetBase:: |
public static | function |
Returns if the widget can be used for the provided field. Overrides WidgetInterface:: |
4 |
WidgetBase:: |
protected | function | Returns whether the widget used for default value form. | |
WidgetBase:: |
public | function |
Massages the form values into the format expected for field values. Overrides WidgetInterface:: |
7 |
WidgetBase:: |
public | function |
Returns a form to configure settings for the widget. Overrides WidgetInterface:: |
16 |
WidgetBase:: |
public | function |
Returns a short summary for the current widget settings. Overrides WidgetInterface:: |
15 |
WidgetBase:: |
public static | function |
Stores processing information about the widget in $form_state. Overrides WidgetBaseInterface:: |
|
WidgetBase:: |
public | function |
Constructs a WidgetBase object. Overrides PluginBase:: |
5 |