View source
<?php
namespace Drupal\inline_entity_form\Plugin\Field\FieldWidget;
use Drupal\Component\Utility\NestedArray;
use Drupal\Component\Utility\Tags;
use Drupal\Core\Entity\Element\EntityAutocomplete;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\Element;
use Drupal\inline_entity_form\TranslationHelper;
use Symfony\Component\DependencyInjection\ContainerInterface;
class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerFactoryPluginInterface {
protected $moduleHandler;
protected $selectionManager;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityTypeManagerInterface $entity_type_manager, EntityDisplayRepositoryInterface $entity_display_repository, ModuleHandlerInterface $module_handler, SelectionPluginManagerInterface $selection_manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings, $entity_type_bundle_info, $entity_type_manager, $entity_display_repository);
$this->moduleHandler = $module_handler;
$this->selectionManager = $selection_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['third_party_settings'], $container
->get('entity_type.bundle.info'), $container
->get('entity_type.manager'), $container
->get('entity_display.repository'), $container
->get('module_handler'), $container
->get('plugin.manager.entity_reference_selection'));
}
public static function defaultSettings() {
$defaults = parent::defaultSettings();
$defaults += [
'allow_new' => TRUE,
'allow_existing' => FALSE,
'match_operator' => 'CONTAINS',
'allow_duplicate' => FALSE,
];
return $defaults;
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = parent::settingsForm($form, $form_state);
$labels = $this
->getEntityTypeLabels();
$states_prefix = 'fields[' . $this->fieldDefinition
->getName() . '][settings_edit_form][settings]';
$element['allow_new'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Allow users to add new @label.', [
'@label' => $labels['plural'],
]),
'#default_value' => $this
->getSetting('allow_new'),
];
$element['allow_existing'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Allow users to add existing @label.', [
'@label' => $labels['plural'],
]),
'#default_value' => $this
->getSetting('allow_existing'),
];
$element['match_operator'] = [
'#type' => 'select',
'#title' => $this
->t('Autocomplete matching'),
'#default_value' => $this
->getSetting('match_operator'),
'#options' => $this
->getMatchOperatorOptions(),
'#description' => $this
->t('Select the method used to collect autocomplete suggestions. Note that <em>Contains</em> can cause performance issues on sites with thousands of nodes.'),
'#states' => [
'visible' => [
':input[name="' . $states_prefix . '[allow_existing]"]' => [
'checked' => TRUE,
],
],
],
];
$element['allow_duplicate'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Allow users to duplicate @label.', [
'@label' => $labels['plural'],
]),
'#default_value' => $this
->getSetting('allow_duplicate'),
];
return $element;
}
public function settingsSummary() {
$summary = parent::settingsSummary();
$labels = $this
->getEntityTypeLabels();
if ($this
->getSetting('allow_new')) {
$summary[] = $this
->t('New @label can be added.', [
'@label' => $labels['plural'],
]);
}
else {
$summary[] = $this
->t('New @label can not be created.', [
'@label' => $labels['plural'],
]);
}
$match_operator_options = $this
->getMatchOperatorOptions();
if ($this
->getSetting('allow_existing')) {
$summary[] = $this
->t('Existing @label can be referenced and are matched with the %operator operator.', [
'@label' => $labels['plural'],
'%operator' => $match_operator_options[$this
->getSetting('match_operator')],
]);
}
else {
$summary[] = $this
->t('Existing @label can not be referenced.', [
'@label' => $labels['plural'],
]);
}
if ($this
->getSetting('allow_duplicate')) {
$summary[] = $this
->t('@label can be duplicated.', [
'@label' => $labels['plural'],
]);
}
else {
$summary[] = $this
->t('@label can not be duplicated.', [
'@label' => $labels['plural'],
]);
}
return $summary;
}
protected function getMatchOperatorOptions() {
return [
'STARTS_WITH' => $this
->t('Starts with'),
'CONTAINS' => $this
->t('Contains'),
];
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$settings = $this
->getSettings();
$target_type = $this
->getFieldSetting('target_type');
$labels = $this
->getEntityTypeLabels();
$parents = array_merge($element['#field_parents'], [
$items
->getName(),
'form',
]);
$this
->setIefId($this
->makeIefId($parents));
$parent_langcode = $items
->getEntity()
->language()
->getId();
$wrapper = 'inline-entity-form-' . $this
->getIefId();
$element = [
'#type' => $this
->getSetting('collapsible') ? 'details' : 'fieldset',
'#tree' => TRUE,
'#description' => $this->fieldDefinition
->getDescription(),
'#prefix' => '<div id="' . $wrapper . '">',
'#suffix' => '</div>',
'#ief_id' => $this
->getIefId(),
'#ief_root' => TRUE,
'#translating' => $this
->isTranslating($form_state),
'#field_title' => $this->fieldDefinition
->getLabel(),
'#after_build' => [
[
get_class($this),
'removeTranslatabilityClue',
],
],
] + $element;
if ($element['#type'] == 'details') {
$element['#open'] = $form_state
->getUserInput() ?: !$this
->getSetting('collapsed');
}
$this
->prepareFormState($form_state, $items, $element['#translating']);
$entities = $form_state
->get([
'inline_entity_form',
$this
->getIefId(),
'entities',
]);
$entities_count = count($entities);
$cardinality = $this->fieldDefinition
->getFieldStorageDefinition()
->getCardinality();
$cardinality_reached = $cardinality > 0 && $entities_count == $cardinality;
$element['#element_validate'][] = [
get_class($this),
'updateRowWeights',
];
if ($element['#required']) {
$element['#element_validate'][] = [
get_class($this),
'requiredField',
];
}
$element['entities'] = [
'#tree' => TRUE,
'#theme' => 'inline_entity_form_entity_table',
'#entity_type' => $target_type,
];
$target_bundles = $this
->getTargetBundles();
$fields = $this->inlineFormHandler
->getTableFields($target_bundles);
$context = [
'parent_entity_type' => $this->fieldDefinition
->getTargetEntityTypeId(),
'parent_bundle' => $this->fieldDefinition
->getTargetBundle(),
'field_name' => $this->fieldDefinition
->getName(),
'entity_type' => $target_type,
'allowed_bundles' => $target_bundles,
];
$this->moduleHandler
->alter('inline_entity_form_table_fields', $fields, $context);
$element['entities']['#table_fields'] = $fields;
$weight_delta = max(ceil($entities_count * 1.2), 50);
foreach ($entities as $key => $value) {
$entity = $value['entity'];
$element['entities'][$key]['#label'] = $this->inlineFormHandler
->getEntityLabel($value['entity']);
$element['entities'][$key]['#entity'] = $value['entity'];
$element['entities'][$key]['#needs_save'] = $value['needs_save'];
$element['entities'][$key]['#weight'] = $value['weight'];
if (!empty($value['form'])) {
$element['entities'][$key]['title'] = [];
$element['entities'][$key]['delta'] = [
'#type' => 'value',
'#value' => $value['weight'],
];
if (in_array($value['form'], [
'edit',
'duplicate',
])) {
$element['entities'][$key]['form'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'ief-form',
'ief-form-row',
],
],
'inline_entity_form' => $this
->getInlineEntityForm($value['form'], $entity
->bundle(), $parent_langcode, $key, array_merge($parents, [
'inline_entity_form',
'entities',
$key,
'form',
]), $value['form'] == 'edit' ? $entity : $entity
->createDuplicate()),
];
$element['entities'][$key]['form']['inline_entity_form']['#process'] = [
[
'\\Drupal\\inline_entity_form\\Element\\InlineEntityForm',
'processEntityForm',
],
[
get_class($this),
'addIefSubmitCallbacks',
],
[
get_class($this),
'buildEntityFormActions',
],
];
}
elseif ($value['form'] == 'remove') {
$element['entities'][$key]['form'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'ief-form',
'ief-form-row',
],
],
'#parents' => array_merge($parents, [
'entities',
$key,
'form',
]),
'#entity' => $entity,
'#ief_id' => $this
->getIefId(),
'#ief_row_delta' => $key,
];
$this
->buildRemoveForm($element['entities'][$key]['form']);
}
}
else {
$row =& $element['entities'][$key];
$row['title'] = [];
$row['delta'] = [
'#type' => 'weight',
'#delta' => $weight_delta,
'#default_value' => $value['weight'],
'#attributes' => [
'class' => [
'ief-entity-delta',
],
],
];
$row['actions'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'ief-entity-operations',
],
],
];
$entity_id = $entity
->id();
if (empty($entity_id) || $entity
->access('update')) {
$row['actions']['ief_entity_edit'] = [
'#type' => 'submit',
'#value' => $this
->t('Edit'),
'#name' => 'ief-' . $this
->getIefId() . '-entity-edit-' . $key,
'#limit_validation_errors' => [],
'#ajax' => [
'callback' => 'inline_entity_form_get_element',
'wrapper' => $wrapper,
],
'#submit' => [
'inline_entity_form_open_row_form',
],
'#ief_row_delta' => $key,
'#ief_row_form' => 'edit',
];
}
if ($settings['allow_duplicate'] && !$cardinality_reached && $entity
->access('create')) {
$row['actions']['ief_entity_duplicate'] = [
'#type' => 'submit',
'#value' => $this
->t('Duplicate'),
'#name' => 'ief-' . $this
->getIefId() . '-entity-duplicate-' . $key,
'#limit_validation_errors' => [
array_merge($parents, [
'actions',
]),
],
'#ajax' => [
'callback' => 'inline_entity_form_get_element',
'wrapper' => $wrapper,
],
'#submit' => [
'inline_entity_form_open_row_form',
],
'#ief_row_delta' => $key,
'#ief_row_form' => 'duplicate',
];
}
if (empty($entity_id) || $settings['allow_existing'] || $entity
->access('delete')) {
$row['actions']['ief_entity_remove'] = [
'#type' => 'submit',
'#value' => $this
->t('Remove'),
'#name' => 'ief-' . $this
->getIefId() . '-entity-remove-' . $key,
'#limit_validation_errors' => [],
'#ajax' => [
'callback' => 'inline_entity_form_get_element',
'wrapper' => $wrapper,
],
'#submit' => [
'inline_entity_form_open_row_form',
],
'#ief_row_delta' => $key,
'#ief_row_form' => 'remove',
'#access' => !$element['#translating'],
];
}
}
}
if ($element['#translating']) {
if (empty($entities)) {
$element['#access'] = FALSE;
}
return $element;
}
if ($cardinality > 1) {
$message = $this
->t('You have added @entities_count out of @cardinality_count allowed @label.', [
'@entities_count' => $entities_count,
'@cardinality_count' => $cardinality,
'@label' => $labels['plural'],
]);
$element['cardinality_count'] = [
'#markup' => '<div class="ief-cardinality-count">' . $message . '</div>',
];
}
if ($cardinality_reached) {
return $element;
}
$create_bundles = $this
->getCreateBundles();
$create_bundles_count = count($create_bundles);
$allow_new = $settings['allow_new'] && !empty($create_bundles);
$hide_cancel = FALSE;
if (empty($entities) && $this->fieldDefinition
->isRequired()) {
if ($settings['allow_existing'] && !$allow_new) {
$form_state
->set([
'inline_entity_form',
$this
->getIefId(),
'form',
], 'ief_add_existing');
$hide_cancel = TRUE;
}
elseif ($create_bundles_count == 1 && $allow_new && !$settings['allow_existing']) {
$bundle = reset($target_bundles);
$parent_entity_type = $this->fieldDefinition
->getTargetEntityTypeId();
$parent_bundle = $this->fieldDefinition
->getTargetBundle();
if ($parent_entity_type != $target_type || $parent_bundle != $bundle) {
$form_state
->set([
'inline_entity_form',
$this
->getIefId(),
'form',
], 'add');
$form_state
->set([
'inline_entity_form',
$this
->getIefId(),
'form settings',
], [
'bundle' => $bundle,
]);
$hide_cancel = TRUE;
}
}
}
$open_form = $form_state
->get([
'inline_entity_form',
$this
->getIefId(),
'form',
]);
if (empty($open_form)) {
$element['actions'] = [
'#attributes' => [
'class' => [
'container-inline',
],
],
'#type' => 'container',
'#weight' => 100,
];
if ($allow_new) {
if ($create_bundles_count > 1) {
$bundles = [];
foreach ($this->entityTypeBundleInfo
->getBundleInfo($target_type) as $bundle_name => $bundle_info) {
if (in_array($bundle_name, $create_bundles)) {
$bundles[$bundle_name] = $bundle_info['label'];
}
}
asort($bundles);
$element['actions']['bundle'] = [
'#type' => 'select',
'#options' => $bundles,
];
}
else {
$element['actions']['bundle'] = [
'#type' => 'value',
'#value' => reset($create_bundles),
];
}
$element['actions']['ief_add'] = [
'#type' => 'submit',
'#value' => $this
->t('Add new @type_singular', [
'@type_singular' => $labels['singular'],
]),
'#name' => 'ief-' . $this
->getIefId() . '-add',
'#limit_validation_errors' => [
array_merge($parents, [
'actions',
]),
],
'#ajax' => [
'callback' => 'inline_entity_form_get_element',
'wrapper' => $wrapper,
],
'#submit' => [
'inline_entity_form_open_form',
],
'#ief_form' => 'add',
];
}
if ($settings['allow_existing']) {
$element['actions']['ief_add_existing'] = [
'#type' => 'submit',
'#value' => $this
->t('Add existing @type_singular', [
'@type_singular' => $labels['singular'],
]),
'#name' => 'ief-' . $this
->getIefId() . '-add-existing',
'#limit_validation_errors' => [
array_merge($parents, [
'actions',
]),
],
'#ajax' => [
'callback' => 'inline_entity_form_get_element',
'wrapper' => $wrapper,
],
'#submit' => [
'inline_entity_form_open_form',
],
'#ief_form' => 'ief_add_existing',
];
}
}
else {
$new_key = $entities ? max(array_keys($entities)) + 1 : 0;
if ($form_state
->get([
'inline_entity_form',
$this
->getIefId(),
'form',
]) == 'add') {
$element['form'] = [
'#type' => 'fieldset',
'#attributes' => [
'class' => [
'ief-form',
'ief-form-bottom',
],
],
'inline_entity_form' => $this
->getInlineEntityForm('add', $this
->determineBundle($form_state), $parent_langcode, $new_key, array_merge($parents, [
$new_key,
])),
];
$element['form']['inline_entity_form']['#process'] = [
[
'\\Drupal\\inline_entity_form\\Element\\InlineEntityForm',
'processEntityForm',
],
[
get_class($this),
'addIefSubmitCallbacks',
],
[
get_class($this),
'buildEntityFormActions',
],
];
}
elseif ($form_state
->get([
'inline_entity_form',
$this
->getIefId(),
'form',
]) == 'ief_add_existing') {
$element['form'] = [
'#type' => 'fieldset',
'#attributes' => [
'class' => [
'ief-form',
'ief-form-bottom',
],
],
'#ief_id' => $this
->getIefId(),
'#parents' => array_merge($parents, [
$new_key,
]),
'#entity_type' => $target_type,
'#ief_labels' => $this
->getEntityTypeLabels(),
'#match_operator' => $this
->getSetting('match_operator'),
];
$element['form'] += inline_entity_form_reference_form($element['form'], $form_state);
}
if ($hide_cancel) {
if ($open_form == 'add') {
$process_element =& $element['form']['inline_entity_form'];
}
elseif ($open_form == 'ief_add_existing') {
$process_element =& $element['form'];
}
$process_element['#process'][] = [
get_class($this),
'hideCancel',
];
}
}
return $element;
}
public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state) {
if ($this
->isDefaultValueWidget($form_state)) {
$items
->filterEmptyItems();
return;
}
$triggering_element = $form_state
->getTriggeringElement();
if (empty($triggering_element['#ief_submit_trigger'])) {
return;
}
$field_name = $this->fieldDefinition
->getName();
$parents = array_merge($form['#parents'], [
$field_name,
'form',
]);
$ief_id = $this
->makeIefId($parents);
$this
->setIefId($ief_id);
$widget_state =& $form_state
->get([
'inline_entity_form',
$ief_id,
]);
foreach ($widget_state['entities'] as $key => $value) {
$changed = TranslationHelper::updateEntityLangcode($value['entity'], $form_state);
if ($changed) {
$widget_state['entities'][$key]['entity'] = $value['entity'];
$widget_state['entities'][$key]['needs_save'] = TRUE;
}
}
$values = $widget_state['entities'];
if (empty($values) && !empty($widget_state['form'])) {
if ($widget_state['form'] == 'add') {
$element = NestedArray::getValue($form, [
$field_name,
'widget',
'form',
]);
$entity = $element['inline_entity_form']['#entity'];
$values[] = [
'entity' => $entity,
];
}
elseif ($widget_state['form'] == 'ief_add_existing') {
$parent = NestedArray::getValue($form, [
$field_name,
'widget',
'form',
]);
$element = isset($parent['entity_id']) ? $parent['entity_id'] : [];
if (!empty($element['#value'])) {
$options = [
'target_type' => $element['#target_type'],
'handler' => $element['#selection_handler'],
] + $element['#selection_settings'];
$handler = $this->selectionManager
->getInstance($options);
$input_values = $element['#tags'] ? Tags::explode($element['#value']) : [
$element['#value'],
];
foreach ($input_values as $input) {
$match = EntityAutocomplete::extractEntityIdFromAutocompleteInput($input);
if ($match === NULL) {
$entities_by_bundle = $handler
->getReferenceableEntities($input, '=');
$entities = array_reduce($entities_by_bundle, function ($flattened, $bundle_entities) {
return $flattened + $bundle_entities;
}, []);
$params = [
'%value' => $input,
'@value' => $input,
];
if (empty($entities)) {
$form_state
->setError($element, $this
->t('There are no entities matching "%value".', $params));
}
elseif (count($entities) > 5) {
$params['@id'] = key($entities);
$form_state
->setError($element, $this
->t('Many entities are called %value. Specify the one you want by appending the id in parentheses, like "@value (@id)".', $params));
}
elseif (count($entities) > 1) {
$multiples = [];
foreach ($entities as $id => $name) {
$multiples[] = $name . ' (' . $id . ')';
}
$params['@id'] = $id;
$form_state
->setError($element, $this
->t('Multiple entities match this reference; "%multiple". Specify the one you want by appending the id in parentheses, like "@value (@id)".', [
'%multiple' => implode('", "', $multiples),
] + $params));
}
else {
$values += [
'target_id' => key($entities),
];
}
}
else {
$values += [
'target_id' => $match,
];
}
}
}
}
}
uasort($values, '\\Drupal\\Component\\Utility\\SortArray::sortByWeightElement');
$values = $this
->massageFormValues($values, $form, $form_state);
$items
->setValue($values);
$items
->filterEmptyItems();
}
public static function buildEntityFormActions($element) {
$delta = $element['#ief_id'];
if ($element['#op'] == 'add') {
$save_label = t('Create @type_singular', [
'@type_singular' => $element['#ief_labels']['singular'],
]);
}
elseif ($element['#op'] == 'duplicate') {
$save_label = t('Duplicate @type_singular', [
'@type_singular' => $element['#ief_labels']['singular'],
]);
}
else {
$delta .= '-' . $element['#ief_row_delta'];
$save_label = t('Update @type_singular', [
'@type_singular' => $element['#ief_labels']['singular'],
]);
}
$element['actions'] = [
'#type' => 'container',
'#weight' => 100,
];
$element['actions']['ief_' . $element['#op'] . '_save'] = [
'#type' => 'submit',
'#value' => $save_label,
'#name' => 'ief-' . $element['#op'] . '-submit-' . $delta,
'#limit_validation_errors' => [
$element['#parents'],
],
'#attributes' => [
'class' => [
'ief-entity-submit',
],
],
'#ajax' => [
'callback' => 'inline_entity_form_get_element',
'wrapper' => 'inline-entity-form-' . $element['#ief_id'],
],
];
$element['actions']['ief_' . $element['#op'] . '_cancel'] = [
'#type' => 'submit',
'#value' => t('Cancel'),
'#name' => 'ief-' . $element['#op'] . '-cancel-' . $delta,
'#limit_validation_errors' => [],
'#ajax' => [
'callback' => 'inline_entity_form_get_element',
'wrapper' => 'inline-entity-form-' . $element['#ief_id'],
],
];
if ($element['#op'] == 'add') {
static::addSubmitCallbacks($element['actions']['ief_add_save']);
$element['actions']['ief_add_cancel']['#submit'] = [
[
get_called_class(),
'closeChildForms',
],
[
get_called_class(),
'closeForm',
],
'inline_entity_form_cleanup_form_state',
];
}
else {
$element['actions']['ief_' . $element['#op'] . '_save']['#ief_row_delta'] = $element['#ief_row_delta'];
$element['actions']['ief_' . $element['#op'] . '_cancel']['#ief_row_delta'] = $element['#ief_row_delta'];
static::addSubmitCallbacks($element['actions']['ief_' . $element['#op'] . '_save']);
$element['actions']['ief_' . $element['#op'] . '_save']['#submit'][] = [
get_called_class(),
'submitCloseRow',
];
$element['actions']['ief_' . $element['#op'] . '_cancel']['#submit'] = [
[
get_called_class(),
'closeChildForms',
],
[
get_called_class(),
'submitCloseRow',
],
'inline_entity_form_cleanup_row_form_state',
];
}
return $element;
}
public static function hideCancel($element) {
if (isset($element['actions']['ief_add_cancel'])) {
$element['actions']['ief_add_cancel']['#access'] = FALSE;
}
elseif (isset($element['actions']['ief_reference_cancel'])) {
$element['actions']['ief_reference_cancel']['#access'] = FALSE;
}
return $element;
}
protected function buildRemoveForm(&$form) {
$entity = $form['#entity'];
$entity_id = $entity
->id();
$entity_label = $this->inlineFormHandler
->getEntityLabel($entity);
$labels = $this
->getEntityTypeLabels();
if ($entity_label) {
$message = $this
->t('Are you sure you want to remove %label?', [
'%label' => $entity_label,
]);
}
else {
$message = $this
->t('Are you sure you want to remove this %entity_type?', [
'%entity_type' => $labels['singular'],
]);
}
$form['message'] = [
'#theme_wrappers' => [
'container',
],
'#markup' => $message,
];
if (!empty($entity_id) && $this
->getSetting('allow_existing') && $entity
->access('delete')) {
$form['delete'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Delete this @type_singular from the system.', [
'@type_singular' => $labels['singular'],
]),
];
}
$delta = $form['#ief_id'] . '-' . $form['#ief_row_delta'];
$form['actions'] = [
'#type' => 'container',
'#weight' => 100,
];
$form['actions']['ief_remove_confirm'] = [
'#type' => 'submit',
'#value' => $this
->t('Remove'),
'#name' => 'ief-remove-confirm-' . $delta,
'#limit_validation_errors' => [
$form['#parents'],
],
'#ajax' => [
'callback' => 'inline_entity_form_get_element',
'wrapper' => 'inline-entity-form-' . $form['#ief_id'],
],
'#allow_existing' => $this
->getSetting('allow_existing'),
'#submit' => [
[
get_class($this),
'submitConfirmRemove',
],
],
'#ief_row_delta' => $form['#ief_row_delta'],
];
$form['actions']['ief_remove_cancel'] = [
'#type' => 'submit',
'#value' => $this
->t('Cancel'),
'#name' => 'ief-remove-cancel-' . $delta,
'#limit_validation_errors' => [],
'#ajax' => [
'callback' => 'inline_entity_form_get_element',
'wrapper' => 'inline-entity-form-' . $form['#ief_id'],
],
'#submit' => [
[
get_class($this),
'submitCloseRow',
],
],
'#ief_row_delta' => $form['#ief_row_delta'],
];
}
public static function submitCloseRow($form, FormStateInterface $form_state) {
$element = inline_entity_form_get_element($form, $form_state);
$ief_id = $element['#ief_id'];
$delta = $form_state
->getTriggeringElement()['#ief_row_delta'];
$form_state
->setRebuild();
$form_state
->set([
'inline_entity_form',
$ief_id,
'entities',
$delta,
'form',
], NULL);
}
public static function submitConfirmRemove($form, FormStateInterface $form_state) {
$element = inline_entity_form_get_element($form, $form_state);
$remove_button = $form_state
->getTriggeringElement();
$delta = $remove_button['#ief_row_delta'];
$entity = $element['entities'][$delta]['form']['#entity'];
$entity_id = $entity
->id();
$form_values = NestedArray::getValue($form_state
->getValues(), $element['entities'][$delta]['form']['#parents']);
$form_state
->setRebuild();
$widget_state = $form_state
->get([
'inline_entity_form',
$element['#ief_id'],
]);
if (empty($entity_id) || $remove_button['#allow_existing'] && empty($form_values['delete'])) {
unset($widget_state['entities'][$delta]);
}
else {
$widget_state['delete'][] = $entity;
unset($widget_state['entities'][$delta]);
}
$form_state
->set([
'inline_entity_form',
$element['#ief_id'],
], $widget_state);
}
protected function determineBundle(FormStateInterface $form_state) {
$ief_settings = $form_state
->get([
'inline_entity_form',
$this
->getIefId(),
]);
if (!empty($ief_settings['form settings']['bundle'])) {
return $ief_settings['form settings']['bundle'];
}
elseif (!empty($ief_settings['bundle'])) {
return $ief_settings['bundle'];
}
else {
$target_bundles = $this
->getTargetBundles();
return reset($target_bundles);
}
}
public static function updateRowWeights($element, FormStateInterface $form_state, $form) {
$ief_id = $element['#ief_id'];
foreach (Element::children($element['entities']) as $key) {
$form_state
->set([
'inline_entity_form',
$ief_id,
'entities',
$key,
'weight',
], $element['entities'][$key]['delta']['#value']);
}
}
public static function requiredField($element, FormStateInterface $form_state, $form) {
$ief_id = $element['#ief_id'];
$children = $form_state
->get([
'inline_entity_form',
$ief_id,
'entities',
]);
$has_children = !empty($children);
$form = $form_state
->get([
'inline_entity_form',
$ief_id,
'form',
]);
$form_open = !empty($form);
if (!$has_children && !$form_open) {
$instance = $form_state
->get([
'inline_entity_form',
$ief_id,
'instance',
]);
$form_state
->setError($element, t('@name field is required.', [
'@name' => $instance
->getLabel(),
]));
}
}
public static function closeForm($form, FormStateInterface $form_state) {
$element = inline_entity_form_get_element($form, $form_state);
$ief_id = $element['#ief_id'];
$form_state
->setRebuild();
$form_state
->set([
'inline_entity_form',
$ief_id,
'form',
], NULL);
}
public static function addSubmitCallbacks(&$element) {
$element['#submit'] = [
[
'\\Drupal\\inline_entity_form\\ElementSubmit',
'trigger',
],
[
'\\Drupal\\inline_entity_form\\Plugin\\Field\\FieldWidget\\InlineEntityFormComplex',
'closeForm',
],
];
$element['#ief_submit_trigger'] = TRUE;
}
public static function closeChildForms($form, FormStateInterface &$form_state) {
$element = inline_entity_form_get_element($form, $form_state);
inline_entity_form_close_all_forms($element, $form_state);
}
}