From 3711a9907307ba9ce066bcd44d133ba51fcc5be6 Mon Sep 17 00:00:00 2001 From: bcweaver <brianweaver@gmail.com> Date: Wed, 24 Oct 2018 13:44:56 -0400 Subject: [PATCH] Update 'inline_entity_form' module: 8.x-1.0-beta1 --> 8.x-1.0-rc1 --- composer.json | 2 +- composer.lock | 16 ++-- vendor/composer/installed.json | 16 ++-- .../schema/inline_entity_form.schema.yml | 15 +++ .../inline_entity_form.info.yml | 6 +- .../inline_entity_form.module | 23 +++-- .../src/Element/InlineEntityForm.php | 58 ++++------- .../src/Form/EntityInlineForm.php | 39 +++++++- .../FieldWidget/InlineEntityFormBase.php | 34 +++++-- .../FieldWidget/InlineEntityFormComplex.php | 82 +++++++++++----- .../FieldWidget/InlineEntityFormSimple.php | 20 +++- .../src/Tests/ComplexWidgetWebTest.php | 96 +++++++++++++------ .../src/Tests/ElementWebTest.php | 2 +- .../src/Tests/InlineEntityFormTestBase.php | 4 +- .../src/Tests/SimpleWidgetWebTest.php | 4 +- .../src/Tests/TranslationTest.php | 2 +- .../src/TranslationHelper.php | 7 +- .../inline_entity_form_test.info.yml | 6 +- .../inline_entity_form_test.routing.yml | 11 +-- .../src/IefEditTest.php | 47 --------- .../inline_entity_form_test/src/IefTest.php | 8 +- 21 files changed, 300 insertions(+), 198 deletions(-) delete mode 100644 web/modules/inline_entity_form/tests/modules/inline_entity_form_test/src/IefEditTest.php diff --git a/composer.json b/composer.json index 131a640f06..0a278b7203 100644 --- a/composer.json +++ b/composer.json @@ -107,7 +107,7 @@ "drupal/google_analytics": "2.2", "drupal/honeypot": "^1.28", "drupal/image_popup": "1.1", - "drupal/inline_entity_form": "1.0-beta1", + "drupal/inline_entity_form": "1.0-rc1", "drupal/link_attributes": "1.0", "drupal/linkit": "5.0-beta6", "drupal/magnific_popup": "1.3", diff --git a/composer.lock b/composer.lock index 97231667f6..1c55f8506f 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "5708485abce6f91ba4060cba02b1f1db", + "content-hash": "660f35d22fb7bba03ecb6151fb4ee806", "packages": [ { "name": "alchemy/zippy", @@ -4094,17 +4094,17 @@ }, { "name": "drupal/inline_entity_form", - "version": "1.0.0-beta1", + "version": "1.0.0-rc1", "source": { "type": "git", "url": "https://git.drupal.org/project/inline_entity_form", - "reference": "8.x-1.0-beta1" + "reference": "8.x-1.0-rc1" }, "dist": { "type": "zip", - "url": "https://ftp.drupal.org/files/projects/inline_entity_form-8.x-1.0-beta1.zip", - "reference": "8.x-1.0-beta1", - "shasum": "185ffc28a7b68d19cce057855d1c111f1741a3ea" + "url": "https://ftp.drupal.org/files/projects/inline_entity_form-8.x-1.0-rc1.zip", + "reference": "8.x-1.0-rc1", + "shasum": "898789fb6a0662fc2572b87f8d0654a0241473f9" }, "require": { "drupal/core": "~8.0" @@ -4118,11 +4118,11 @@ "dev-1.x": "1.x-dev" }, "drupal": { - "version": "8.x-1.0-beta1", + "version": "8.x-1.0-rc1", "datestamp": "1527030784", "security-coverage": { "status": "not-covered", - "message": "Beta releases are not covered by Drupal security advisories." + "message": "RC releases are not covered by Drupal security advisories." } } }, diff --git a/vendor/composer/installed.json b/vendor/composer/installed.json index 9ec806fba6..14f81b8abc 100644 --- a/vendor/composer/installed.json +++ b/vendor/composer/installed.json @@ -4219,18 +4219,18 @@ }, { "name": "drupal/inline_entity_form", - "version": "1.0.0-beta1", - "version_normalized": "1.0.0.0-beta1", + "version": "1.0.0-rc1", + "version_normalized": "1.0.0.0-RC1", "source": { "type": "git", "url": "https://git.drupal.org/project/inline_entity_form", - "reference": "8.x-1.0-beta1" + "reference": "8.x-1.0-rc1" }, "dist": { "type": "zip", - "url": "https://ftp.drupal.org/files/projects/inline_entity_form-8.x-1.0-beta1.zip", - "reference": "8.x-1.0-beta1", - "shasum": "185ffc28a7b68d19cce057855d1c111f1741a3ea" + "url": "https://ftp.drupal.org/files/projects/inline_entity_form-8.x-1.0-rc1.zip", + "reference": "8.x-1.0-rc1", + "shasum": "898789fb6a0662fc2572b87f8d0654a0241473f9" }, "require": { "drupal/core": "~8.0" @@ -4244,11 +4244,11 @@ "dev-1.x": "1.x-dev" }, "drupal": { - "version": "8.x-1.0-beta1", + "version": "8.x-1.0-rc1", "datestamp": "1527030784", "security-coverage": { "status": "not-covered", - "message": "Beta releases are not covered by Drupal security advisories." + "message": "RC releases are not covered by Drupal security advisories." } } }, diff --git a/web/modules/inline_entity_form/config/schema/inline_entity_form.schema.yml b/web/modules/inline_entity_form/config/schema/inline_entity_form.schema.yml index a522ec84cb..aef7d466ed 100644 --- a/web/modules/inline_entity_form/config/schema/inline_entity_form.schema.yml +++ b/web/modules/inline_entity_form/config/schema/inline_entity_form.schema.yml @@ -25,6 +25,12 @@ field.widget.settings.inline_entity_form_simple: match_operator: type: string label: "Match operator" + collapsible: + type: boolean + label: "Collapsible" + collapsed: + type: boolean + label: "Collapsed by default" field.widget.settings.inline_entity_form_complex: type: mapping @@ -51,3 +57,12 @@ field.widget.settings.inline_entity_form_complex: match_operator: type: string label: "Match operator" + allow_duplicate: + type: boolean + label: "Allow duplicate" + collapsible: + type: boolean + label: "Collapsible" + collapsed: + type: boolean + label: "Collapsed by default" diff --git a/web/modules/inline_entity_form/inline_entity_form.info.yml b/web/modules/inline_entity_form/inline_entity_form.info.yml index a70d040a9c..d4b6a49d2c 100644 --- a/web/modules/inline_entity_form/inline_entity_form.info.yml +++ b/web/modules/inline_entity_form/inline_entity_form.info.yml @@ -6,8 +6,8 @@ package: Fields test_dependencies: - entity_reference_revisions:entity_reference_revisions -# Information added by Drupal.org packaging script on 2016-10-30 -version: '8.x-1.0-beta1' +# Information added by Drupal.org packaging script on 2018-05-22 +version: '8.x-1.0-rc1' core: '8.x' project: 'inline_entity_form' -datestamp: 1477868362 +datestamp: 1527030788 diff --git a/web/modules/inline_entity_form/inline_entity_form.module b/web/modules/inline_entity_form/inline_entity_form.module index 056ee9d440..7e1e50803e 100644 --- a/web/modules/inline_entity_form/inline_entity_form.module +++ b/web/modules/inline_entity_form/inline_entity_form.module @@ -72,14 +72,21 @@ function inline_entity_form_reference_form($reference_form, &$form_state) { $ief_id = $reference_form['#ief_id']; /** @var \Drupal\field\Entity\FieldConfig $instance */ $instance = $form_state->get(['inline_entity_form', $ief_id, 'instance']); + $selection_settings = [ + 'match_operator' => $reference_form['#match_operator'], + ] + $instance->getSetting('handler_settings'); $reference_form['#title'] = t('Add existing @type_singular', ['@type_singular' => $labels['singular']]); + $reference_form['entity_id'] = [ '#type' => 'entity_autocomplete', - '#title' => t('@label', ['@label' => ucwords($labels['singular'])]), + // @todo Use bundle defined singular/plural labels as soon as + // https://www.drupal.org/node/2765065 is committed. + // @see https://www.drupal.org/node/2765065 + '#title' => t('@label', ['@label' => ucfirst($labels['singular'])]), '#target_type' => $instance->getSetting('target_type'), '#selection_handler' => $instance->getSetting('handler'), - '#selection_settings' => $instance->getSetting('handler_settings'), + '#selection_settings' => $selection_settings, '#required' => TRUE, '#maxlength' => 255, ]; @@ -125,7 +132,7 @@ function inline_entity_form_reference_form($reference_form, &$form_state) { * Validates the form for adding existing entities. * * @param array $reference_form - * The reference entity form. + * The reference entity form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The form state of the parent form. */ @@ -158,7 +165,7 @@ function inline_entity_form_reference_form_validate(&$reference_form, FormStateI * Adds the specified entity to the IEF form state. * * @param array $reference_form - * The reference entity form. + * The reference entity form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The form state of the parent form. */ @@ -334,10 +341,10 @@ function theme_inline_entity_form_entity_table($variables) { // Add header columns for each field. $first = TRUE; foreach ($fields as $field_name => $field) { - $column = ['data' => $field['label']]; + $column = ['data' => $field['label'], 'class' => ['inline-entity-form-' . $entity_type . '-' . $field_name]]; // The first column gets a special class. if ($first) { - $column['class'] = ['ief-first-column-header']; + $column['class'][] = 'ief-first-column-header'; $first = FALSE; } $header[] = $column; @@ -363,7 +370,6 @@ function theme_inline_entity_form_entity_table($variables) { } foreach ($fields as $field_name => $field) { - $data = ''; if ($field['type'] == 'label') { $data = $variables['form'][$key]['#label']; } @@ -385,6 +391,9 @@ function theme_inline_entity_form_entity_table($variables) { $data = call_user_func_array($field['callback'], $arguments); } + else { + $data = t('N/A'); + } $cells[] = ['data' => $data, 'class' => ['inline-entity-form-' . $entity_type . '-' . $field_name]]; } diff --git a/web/modules/inline_entity_form/src/Element/InlineEntityForm.php b/web/modules/inline_entity_form/src/Element/InlineEntityForm.php index b55f7812c8..0fbe2335ab 100644 --- a/web/modules/inline_entity_form/src/Element/InlineEntityForm.php +++ b/web/modules/inline_entity_form/src/Element/InlineEntityForm.php @@ -47,9 +47,14 @@ public function getInfo() { '#form_mode' => 'default', // Will save entity on submit if set to TRUE. '#save_entity' => TRUE, - // 'add' or 'edit'. If NULL, determined by whether the entity is new. + // 'add', 'edit' or 'duplicate'. '#op' => NULL, '#process' => [ + // Core's #process for groups, don't remove it. + [$class, 'processGroup'], + + // InlineEntityForm's #process must run after the above ::processGroup + // in case any new elements (like groups) were added in alter hooks. [$class, 'processEntityForm'], ], '#element_validate' => [ @@ -59,9 +64,10 @@ public function getInfo() { [$class, 'submitEntityForm'], ], '#theme_wrappers' => ['container'], - // Allow inline forms to use the #fieldset key. + '#pre_render' => [ - [$class, 'addFieldsetMarkup'], + // Core's #pre_render for groups, don't remove it. + [$class, 'preRenderGroup'], ], ]; } @@ -114,7 +120,13 @@ public static function processEntityForm($entity_form, FormStateInterface $form_ $entity_form['#entity'] = $storage->create($values); } if (!isset($entity_form['#op'])) { - $entity_form['#op'] = $entity_form['#entity']->isNew() ? 'add' : 'edit'; + // When duplicating entities, the entity is new, but already has a UUID. + if ($entity_form['#entity']->isNew() && $entity_form['#entity']->uuid()) { + $entity_form['#op'] = 'duplicate'; + } + else { + $entity_form['#op'] = $entity_form['#entity']->isNew() ? 'add' : 'edit'; + } } // Prepare the entity form and the entity itself for translating. $entity_form['#entity'] = TranslationHelper::prepareEntity($entity_form['#entity'], $form_state); @@ -179,42 +191,4 @@ public static function getInlineFormHandler($entity_type) { return $inline_form_handler; } - /** - * Pre-render callback for the #fieldset form property. - * - * Inline forms use #tree = TRUE to keep their values in a hierarchy for - * easier storage. Moving the form elements into fieldsets during form - * building would break up that hierarchy, so it's not an option for entity - * fields. Therefore, we wait until the pre_render stage, where any changes - * we make affect presentation only and aren't reflected in $form_state. - * - * @param array $entity_form - * The entity form. - * - * @return array - * The modified entity form. - */ - public static function addFieldsetMarkup($entity_form) { - $sort = []; - foreach (Element::children($entity_form) as $key) { - $element = $entity_form[$key]; - if (isset($element['#fieldset']) && isset($entity_form[$element['#fieldset']])) { - $entity_form[$element['#fieldset']][$key] = $element; - // Remove the original element this duplicates. - unset($entity_form[$key]); - // Mark the fieldset for sorting. - if (!in_array($key, $sort)) { - $sort[] = $element['#fieldset']; - } - } - } - - // Sort all fieldsets, so that element #weight stays respected. - foreach ($sort as $key) { - uasort($entity_form[$key], '\Drupal\Component\Utility\SortArray::sortByWeightProperty'); - } - - return $entity_form; - } - } diff --git a/web/modules/inline_entity_form/src/Form/EntityInlineForm.php b/web/modules/inline_entity_form/src/Form/EntityInlineForm.php index 06cbbdc0b8..1ff5073792 100644 --- a/web/modules/inline_entity_form/src/Form/EntityInlineForm.php +++ b/web/modules/inline_entity_form/src/Form/EntityInlineForm.php @@ -182,9 +182,46 @@ public function entityForm(array $entity_form, FormStateInterface $form_state) { } } } + + // Determine the children of the entity form before it has been altered. + $children_before = Element::children($entity_form); + // Allow other modules to alter the form. $this->moduleHandler->alter('inline_entity_form_entity_form', $entity_form, $form_state); + // Determine the children of the entity form after it has been altered. + $children_after = Element::children($entity_form); + + // Ensure that any new children added have #tree, #parents, #array_parents + // and handle setting the proper #group if it's referencing a local element. + // Note: the #tree, #parents and #array_parents code is a direct copy from + // \Drupal\Core\Form\FormBuilder::doBuildForm. + $children_diff = array_diff($children_after, $children_before); + foreach ($children_diff as $child) { + // Don't squash an existing tree value. + if (!isset($entity_form[$child]['#tree'])) { + $entity_form[$child]['#tree'] = $entity_form['#tree']; + } + + // Don't squash existing parents value. + if (!isset($entity_form[$child]['#parents'])) { + // Check to see if a tree of child elements is present. If so, + // continue down the tree if required. + $entity_form[$child]['#parents'] = $entity_form[$child]['#tree'] && $entity_form['#tree'] ? array_merge($entity_form['#parents'], [$child]) : [$child]; + } + + // Ensure #array_parents follows the actual form structure. + $array_parents = $entity_form['#array_parents']; + $array_parents[] = $child; + $entity_form[$child]['#array_parents'] = $array_parents; + + // Detect if there is a #group and it specifies a local element. If so, + // change it to use the proper local element's #parents group name. + if (isset($entity_form[$child]['#group']) && isset($entity_form[$entity_form[$child]['#group']])) { + $entity_form[$child]['#group'] = implode('][', $entity_form[$entity_form[$child]['#group']]['#parents']); + } + } + return $entity_form; } @@ -203,7 +240,7 @@ public function entityFormValidate(array &$entity_form, FormStateInterface $form $form_display->validateFormValues($entity, $entity_form, $form_state); $entity->setValidationRequired(FALSE); - foreach($form_state->getErrors() as $name => $message) { + foreach ($form_state->getErrors() as $name => $message) { // $name may be unknown in $form_state and // $form_state->setErrorByName($name, $message) may suppress the error message. $form_state->setError($triggering_element, $message); diff --git a/web/modules/inline_entity_form/src/Plugin/Field/FieldWidget/InlineEntityFormBase.php b/web/modules/inline_entity_form/src/Plugin/Field/FieldWidget/InlineEntityFormBase.php index 123b58fc65..1e69c7c9fb 100644 --- a/web/modules/inline_entity_form/src/Plugin/Field/FieldWidget/InlineEntityFormBase.php +++ b/web/modules/inline_entity_form/src/Plugin/Field/FieldWidget/InlineEntityFormBase.php @@ -2,7 +2,6 @@ namespace Drupal\inline_entity_form\Plugin\Field\FieldWidget; -use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityDisplayRepositoryInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeBundleInfoInterface; @@ -72,7 +71,7 @@ abstract class InlineEntityFormBase extends WidgetBase implements ContainerFacto * The entity type bundle info. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity type manager. - * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface + * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository * The entity display repository. */ 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) { @@ -194,6 +193,8 @@ public static function defaultSettings() { 'override_labels' => FALSE, 'label_singular' => '', 'label_plural' => '', + 'collapsible' => FALSE, + 'collapsed' => FALSE, ]; } @@ -236,6 +237,21 @@ public function settingsForm(array $form, FormStateInterface $form_state) { ], ], ]; + $element['collapsible'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Collapsible'), + '#default_value' => $this->getSetting('collapsible'), + ]; + $element['collapsed'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Collapsed by default'), + '#default_value' => $this->getSetting('collapsed'), + '#states' => [ + 'visible' => [ + ':input[name="' . $states_prefix . '[collapsible]"]' => ['checked' => TRUE], + ], + ], + ]; return $element; } @@ -262,6 +278,10 @@ public function settingsSummary() { $summary[] = $this->t('Default labels are used.'); } + if ($this->getSetting('collapsible')) { + $summary[] = $this->t($this->getSetting('collapsed') ? 'Collapsible, collapsed by default' : 'Collapsible'); + } + return $summary; } @@ -291,7 +311,7 @@ protected function getEntityTypeLabels() { * - Is IEF handler loaded? * - Are we on a "real" entity form and not on default value widget? * - * @param FormStateInterface $form_state + * @param \Drupal\Core\Form\FormStateInterface $form_state * Form state. * * @return bool @@ -404,7 +424,7 @@ protected function getInlineEntityForm($operation, $bundle, $langcode, $delta, a * @return bool * TRUE if translating is in progress, FALSE otherwise. * - * @see \Drupal\inline_entity_form\TranslationHelper::initFormLangcodes(). + * @see \Drupal\inline_entity_form\TranslationHelper::initFormLangcodes() */ protected function isTranslating(FormStateInterface $form_state) { if (TranslationHelper::isTranslating($form_state)) { @@ -453,8 +473,8 @@ public static function addIefSubmitCallbacks($element) { * still decide to cancel the parent form. * * @param $entity_form - * The form of the entity being managed inline. - * @param $form_state + * The form of the entity being managed inline. + * @param \Drupal\Core\Form\FormStateInterface $form_state * The form state of the parent form. */ public static function submitSaveEntity($entity_form, FormStateInterface $form_state) { @@ -462,7 +482,7 @@ public static function submitSaveEntity($entity_form, FormStateInterface $form_s /** @var \Drupal\Core\Entity\EntityInterface $entity */ $entity = $entity_form['#entity']; - if ($entity_form['#op'] == 'add') { + if (in_array($entity_form['#op'], ['add', 'duplicate'])) { // Determine the correct weight of the new element. $weight = 0; $entities = $form_state->get(['inline_entity_form', $ief_id, 'entities']); diff --git a/web/modules/inline_entity_form/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php b/web/modules/inline_entity_form/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php index 92a52feec5..b8424be3b6 100644 --- a/web/modules/inline_entity_form/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php +++ b/web/modules/inline_entity_form/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php @@ -37,7 +37,7 @@ class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerF protected $moduleHandler; /** - * Constructs a InlineEntityFormBase object. + * Constructs a InlineEntityFormComplex object. * * @param array $plugin_id * The plugin_id for the widget. @@ -53,7 +53,7 @@ class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerF * The entity type bundle info. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity type manager. - * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface + * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository * The entity display repository. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * Module handler service. @@ -89,6 +89,7 @@ public static function defaultSettings() { 'allow_new' => TRUE, 'allow_existing' => FALSE, 'match_operator' => 'CONTAINS', + 'allow_duplicate' => FALSE, ]; return $defaults; @@ -124,6 +125,11 @@ public function settingsForm(array $form, FormStateInterface $form_state) { ], ], ]; + $element['allow_duplicate'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Allow users to duplicate @label.', ['@label' => $labels['plural']]), + '#default_value' => $this->getSetting('allow_duplicate'), + ]; return $element; } @@ -153,6 +159,13 @@ public function settingsSummary() { $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; } @@ -196,7 +209,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen $wrapper = 'inline-entity-form-' . $this->getIefId(); $element = [ - '#type' => 'fieldset', + '#type' => $this->getSetting('collapsible') ? 'details' : 'fieldset', '#tree' => TRUE, '#description' => $this->fieldDefinition->getDescription(), '#prefix' => '<div id="' . $wrapper . '">', @@ -209,12 +222,20 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen [get_class($this), 'removeTranslatabilityClue'], ], ] + $element; + if ($element['#type'] == 'details') { + $element['#open'] = !$this->getSetting('collapsed'); + } $element['#attached']['library'][] = 'inline_entity_form/widget'; $this->prepareFormState($form_state, $items, $element['#translating']); $entities = $form_state->get(['inline_entity_form', $this->getIefId(), 'entities']); + // Prepare cardinality information. + $entities_count = count($entities); + $cardinality = $this->fieldDefinition->getFieldStorageDefinition()->getCardinality(); + $cardinality_reached = ($cardinality > 0 && $entities_count == $cardinality); + // Build the "Multiple value" widget. // TODO - does this belong in #element_validate? $element['#element_validate'][] = [get_class($this), 'updateRowWeights']; @@ -242,7 +263,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen $this->moduleHandler->alter('inline_entity_form_table_fields', $fields, $context); $element['entities']['#table_fields'] = $fields; - $weight_delta = max(ceil(count($entities) * 1.2), 50); + $weight_delta = max(ceil($entities_count * 1.2), 50); foreach ($entities as $key => $value) { // Data used by theme_inline_entity_form_entity_table(). /** @var \Drupal\Core\Entity\EntityInterface $entity */ @@ -263,7 +284,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen ]; // Add the appropriate form. - if ($value['form'] == 'edit') { + if (in_array($value['form'], ['edit', 'duplicate'])) { $element['entities'][$key]['form'] = [ '#type' => 'container', '#attributes' => ['class' => ['ief-form', 'ief-form-row']], @@ -272,8 +293,8 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen $entity->bundle(), $parent_langcode, $key, - array_merge($parents, ['inline_entity_form', 'entities', $key, 'form']), - $entity + array_merge($parents, ['inline_entity_form', 'entities', $key, 'form']), + $value['form'] == 'edit' ? $entity : $entity->createDuplicate() ), ]; @@ -333,6 +354,23 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen ]; } + // Add the duplicate button, if allowed. + 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 'allow_existing' is on, the default removal operation is unlink // and the access check for deleting happens inside the controller // removeForm() method. @@ -365,8 +403,6 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen return $element; } - $entities_count = count($entities); - $cardinality = $this->fieldDefinition->getFieldStorageDefinition()->getCardinality(); if ($cardinality > 1) { // Add a visual cue of cardinality count. $message = $this->t('You have added @entities_count out of @cardinality_count allowed @label.', [ @@ -379,7 +415,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen ]; } // Do not return the rest of the form if cardinality count has been reached. - if ($cardinality > 0 && $entities_count == $cardinality) { + if ($cardinality_reached) { return $element; } @@ -399,7 +435,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen // The parent entity type and bundle must not be the same as the inline // entity type and bundle, to prevent recursion. $parent_entity_type = $this->fieldDefinition->getTargetEntityTypeId(); - $parent_bundle = $this->fieldDefinition->getTargetBundle(); + $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'], [ @@ -502,10 +538,9 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen // Used by Field API and controller methods to find the relevant // values in $form_state. '#parents' => array_merge($parents), - // Pass the current entity type. '#entity_type' => $target_type, - // Pass the widget specific labels. '#ief_labels' => $this->getEntityTypeLabels(), + '#match_operator' => $this->getSetting('match_operator'), ]; $element['form'] += inline_entity_form_reference_form($element['form'], $form_state); @@ -591,6 +626,9 @@ public static function buildEntityFormActions($element) { 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']]); @@ -633,12 +671,12 @@ public static function buildEntityFormActions($element) { ]; } else { - $element['actions']['ief_edit_save']['#ief_row_delta'] = $element['#ief_row_delta']; - $element['actions']['ief_edit_cancel']['#ief_row_delta'] = $element['#ief_row_delta']; + $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_edit_save']); - $element['actions']['ief_edit_save']['#submit'][] = [get_called_class(), 'submitCloseRow']; - $element['actions']['ief_edit_cancel']['#submit'] = [ + 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', @@ -741,7 +779,7 @@ protected function buildRemoveForm(&$form) { * @param $form_state * The form state of the parent form. * - * @see inline_entity_form_open_row_form(). + * @see inline_entity_form_open_row_form() */ public static function submitCloseRow($form, FormStateInterface $form_state) { $element = inline_entity_form_get_element($form, $form_state); @@ -796,7 +834,7 @@ public static function submitConfirmRemove($form, FormStateInterface $form_state /** * Determines bundle to be used when creating entity. * - * @param FormStateInterface $form_state + * @param \Drupal\Core\Form\FormStateInterface $form_state * Current form state. * * @return string @@ -858,7 +896,7 @@ public static function requiredField($element, FormStateInterface $form_state, $ * @param $form_state * The form state of the parent form. * - * @see inline_entity_form_open_form(). + * @see inline_entity_form_open_form() */ public static function closeForm($form, FormStateInterface $form_state) { $element = inline_entity_form_get_element($form, $form_state); @@ -889,7 +927,7 @@ public static function addSubmitCallbacks(&$element) { * * @param $form * The IEF Form element. - * @param FormStateInterface $form_state + * @param \Drupal\Core\Form\FormStateInterface $form_state * The form state of the parent form. */ public static function closeChildForms($form, FormStateInterface &$form_state) { diff --git a/web/modules/inline_entity_form/src/Plugin/Field/FieldWidget/InlineEntityFormSimple.php b/web/modules/inline_entity_form/src/Plugin/Field/FieldWidget/InlineEntityFormSimple.php index 87e7792291..985e06bc6f 100644 --- a/web/modules/inline_entity_form/src/Plugin/Field/FieldWidget/InlineEntityFormSimple.php +++ b/web/modules/inline_entity_form/src/Plugin/Field/FieldWidget/InlineEntityFormSimple.php @@ -36,12 +36,16 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen $form_state->set(['inline_entity_form', $ief_id], []); $element = [ - '#type' => 'fieldset', + '#type' => $this->getSetting('collapsible') ? 'details' : 'fieldset', '#field_title' => $this->fieldDefinition->getLabel(), '#after_build' => [ [get_class($this), 'removeTranslatabilityClue'], ], ] + $element; + if ($element['#type'] == 'details') { + $element['#open'] = !$this->getSetting('collapsed'); + } + $item = $items->get($delta); if ($item->target_id && !$item->entity) { $element['warning']['#markup'] = $this->t('Unable to load the referenced entity.'); @@ -55,7 +59,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen $delta, 'inline_entity_form' ]); - $bundle = !empty($this->getFieldSetting('handler_settings')['target_bundles']) ? reset($this->getFieldSetting('handler_settings')['target_bundles']) : NULL; + $bundle = $this->getBundle(); $element['inline_entity_form'] = $this->getInlineEntityForm($op, $bundle, $langcode, $delta, $parents, $entity); if ($op == 'edit') { @@ -196,4 +200,16 @@ public static function isApplicable(FieldDefinitionInterface $field_definition) return TRUE; } + /** + * Gets the bundle for the inline entity. + * + * @return string|null + * The bundle, or NULL if not known. + */ + protected function getBundle() { + if (!empty($this->getFieldSetting('handler_settings')['target_bundles'])) { + return reset($this->getFieldSetting('handler_settings')['target_bundles']); + } + } + } diff --git a/web/modules/inline_entity_form/src/Tests/ComplexWidgetWebTest.php b/web/modules/inline_entity_form/src/Tests/ComplexWidgetWebTest.php index 974c8a05a0..d606c841b1 100644 --- a/web/modules/inline_entity_form/src/Tests/ComplexWidgetWebTest.php +++ b/web/modules/inline_entity_form/src/Tests/ComplexWidgetWebTest.php @@ -66,7 +66,7 @@ protected function setUp() { */ public function testEmptyFieldIEF() { // Don't allow addition of existing nodes. - $this->setAllowExisting(FALSE); + $this->updateSetting('allow_existing', FALSE); $this->drupalGet($this->formContentAddUrl); $this->assertFieldByName('multi[form][inline_entity_form][title][0][value]', NULL, 'Title field on inline form exists.'); @@ -75,7 +75,7 @@ public function testEmptyFieldIEF() { $this->assertFieldByXpath('//input[@type="submit" and @value="Create node"]', NULL, 'Found "Create node" submit button'); // Allow addition of existing nodes. - $this->setAllowExisting(TRUE); + $this->updateSetting('allow_existing', TRUE); $this->drupalGet($this->formContentAddUrl); $this->assertNoFieldByName('multi[form][inline_entity_form][title][0][value]', NULL, 'Title field does not appear.'); @@ -107,7 +107,7 @@ public function testEmptyFieldIEF() { */ public function testEntityCreation() { // Allow addition of existing nodes. - $this->setAllowExisting(TRUE); + $this->updateSetting('allow_existing', TRUE); $this->drupalGet($this->formContentAddUrl); $this->drupalPostAjaxForm(NULL, [], $this->getButtonName('//input[@type="submit" and @value="Add new node" and @data-drupal-selector="edit-multi-actions-ief-add"]')); @@ -183,7 +183,6 @@ public function testNestedEntityCreationWithDifferentBundlesAjaxSubmit() { foreach ($required_possibilities as $required) { $this->setupNestedComplexForm($required); - $nested3_title = 'nested3 title steps ' . ($required ? 'required' : 'not required'); $nested2_title = 'nested2 title steps ' . ($required ? 'required' : 'not required'); $nested1_title = 'nested1 title steps ' . ($required ? 'required' : 'not required'); @@ -213,10 +212,9 @@ public function testNestedEntityCreationWithDifferentBundlesAjaxSubmit() { * Checks that nested IEF entity references can be edit and saved. * * @param \Drupal\node\Entity\Node $node - * Top level node of type ief_test_nested1 to check. + * Top level node of type ief_test_nested1 to check. * @param bool $ajax_submit - * Whether IEF form widgets should be submitted via AJax or left open. - * + * Whether IEF form widgets should be submitted via AJAX or left open. */ protected function checkNestedEntityEditing(Node $node, $ajax_submit = TRUE) { $this->drupalGet("node/{$node->id()}/edit"); @@ -225,14 +223,14 @@ protected function checkNestedEntityEditing(Node $node, $ajax_submit = TRUE) { /** @var \Drupal\node\Entity\Node $level_2_node */ $level_2_node = $node->test_ref_nested1->entity->test_ref_nested2->entity; $level_2_node_update_title = $level_2_node->getTitle() . ' - updated'; - //edit-test-ref-nested1-entities-0-actions-ief-entity-edit + // edit-test-ref-nested1-entities-0-actions-ief-entity-edit $this->drupalPostAjaxForm(NULL, [], $this->getButtonName('//input[@type="submit" and @data-drupal-selector="edit-test-ref-nested1-entities-0-actions-ief-entity-edit"]')); - //edit-test-ref-nested1-form-inline-entity-form-entities-0-form-test-ref-nested2-entities-0-actions-ief-entity-edit + // edit-test-ref-nested1-form-inline-entity-form-entities-0-form-test-ref-nested2-entities-0-actions-ief-entity-edit $this->drupalPostAjaxForm(NULL, [], $this->getButtonName('//input[@type="submit" and @data-drupal-selector="edit-test-ref-nested1-form-inline-entity-form-entities-0-form-test-ref-nested2-entities-0-actions-ief-entity-edit"]')); $edit['test_ref_nested1[form][inline_entity_form][entities][0][form][test_ref_nested2][form][inline_entity_form][entities][0][form][title][0][value]'] = $level_2_node_update_title; if ($ajax_submit) { // Close IEF Forms with AJAX posts - //edit-test-ref-nested1-form-inline-entity-form-entities-0-form-test-ref-nested2-form-inline-entity-form-entities-0-form-actions-ief-edit-save + // edit-test-ref-nested1-form-inline-entity-form-entities-0-form-test-ref-nested2-form-inline-entity-form-entities-0-form-actions-ief-edit-save $this->drupalPostAjaxForm(NULL, $edit, $this->getButtonName('//input[@type="submit" and @data-drupal-selector="edit-test-ref-nested1-form-inline-entity-form-entities-0-form-test-ref-nested2-form-inline-entity-form-entities-0-form-actions-ief-edit-save"]')); $this->drupalPostAjaxForm(NULL, [], $this->getButtonName('//input[@type="submit" and @data-drupal-selector="edit-test-ref-nested1-form-inline-entity-form-entities-0-form-actions-ief-edit-save"]')); $this->drupalPostForm(NULL, [], t('Save')); @@ -278,7 +276,7 @@ public function testNestedEntityCreationWithDifferentBundlesNoAjaxSubmit() { */ public function testEntityEditingAndRemoving() { // Allow addition of existing nodes. - $this->setAllowExisting(TRUE); + $this->updateSetting('allow_existing', TRUE); // Create three ief_reference_type entities. $referenceNodes = $this->createReferenceContent(3); @@ -291,7 +289,7 @@ public function testEntityEditingAndRemoving() { $parent_node = $this->drupalGetNodeByTitle('Some title'); // Edit the second entity. - $this->drupalGet('node/'. $parent_node->id() .'/edit'); + $this->drupalGet('node/' . $parent_node->id() . '/edit'); $cell = $this->xpath('//table[@id="ief-entity-table-edit-multi-entities"]/tbody/tr[@class="ief-row-entity draggable even"]/td[@class="inline-entity-form-node-label"]'); $title = (string) $cell[0]; @@ -315,7 +313,7 @@ public function testEntityEditingAndRemoving() { $this->assertTrue($node->last_name->value == 'Doe', 'Last name in reference node changed to Doe'); // Delete the second entity. - $this->drupalGet('node/'. $parent_node->id() .'/edit'); + $this->drupalGet('node/' . $parent_node->id() . '/edit'); $cell = $this->xpath('//table[@id="ief-entity-table-edit-multi-entities"]/tbody/tr[@class="ief-row-entity draggable even"]/td[@class="inline-entity-form-node-label"]'); $title = (string) $cell[0]; @@ -335,13 +333,13 @@ public function testEntityEditingAndRemoving() { $this->assertTrue(empty($deleted_node), 'The inline entity was deleted from the site.'); // Checks that entity does nor appear in IEF. - $this->drupalGet('node/'. $parent_node->id() .'/edit'); + $this->drupalGet('node/' . $parent_node->id() . '/edit'); $this->assertNoText($title, 'Deleted inline entity is not present on the page after saving parent.'); // Delete the third entity reference only, don't delete the node. The third // entity now is second referenced entity because the second one was deleted // in previous step. - $this->drupalGet('node/'. $parent_node->id() .'/edit'); + $this->drupalGet('node/' . $parent_node->id() . '/edit'); $cell = $this->xpath('//table[@id="ief-entity-table-edit-multi-entities"]/tbody/tr[@class="ief-row-entity draggable even"]/td[@class="inline-entity-form-node-label"]'); $title = (string) $cell[0]; @@ -356,7 +354,7 @@ public function testEntityEditingAndRemoving() { $this->assertResponse(200, 'Saving parent node was successful.'); // Checks that entity does nor appear in IEF. - $this->drupalGet('node/'. $parent_node->id() . '/edit'); + $this->drupalGet('node/' . $parent_node->id() . '/edit'); $this->assertNoText($title, 'Deleted inline entity is not present on the page after saving parent.'); // Checks that entity is not deleted. @@ -369,7 +367,7 @@ public function testEntityEditingAndRemoving() { */ public function testReferencingExistingEntities() { // Allow addition of existing nodes. - $this->setAllowExisting(TRUE); + $this->updateSetting('allow_existing', TRUE); // Create three ief_reference_type entities. $referenceNodes = $this->createReferenceContent(3); @@ -417,10 +415,10 @@ public function testReferencingExistingEntities() { $this->assertResponse(200, 'Saving parent for was successful.'); // Check if entities are referenced. - $this->drupalGet('node/'. $parent_node->id() .'/edit'); + $this->drupalGet('node/' . $parent_node->id() . '/edit'); for ($i = 2; $i <= 3; $i++) { $cell = $this->xpath('//table[@id="ief-entity-table-edit-multi-entities"]/tbody/tr[' . $i . ']/td[@class="inline-entity-form-node-label"]'); - $this->assertTrue($cell[0] == 'Some reference ' . $i, 'Found reference node title "Some reference ' . $i .'" in the IEF table.'); + $this->assertTrue($cell[0] == 'Some reference ' . $i, 'Found reference node title "Some reference ' . $i . '" in the IEF table.'); } // Check if all remaining nodes from all bundles are referenced. $count = 2; @@ -437,7 +435,7 @@ public function testReferencingExistingEntities() { * Also checks if existing entity reference form can be canceled. */ public function testReferenceExistingValidation() { - $this->setAllowExisting(TRUE); + $this->updateSetting('allow_existing', TRUE); $this->drupalGet('node/add/ief_test_complex'); $this->checkExistingValidationExpectation('', 'Node field is required.'); @@ -465,12 +463,50 @@ public function testReferenceExistingValidation() { } } + /** + * Tests if duplicating entities works. + */ + public function testDuplicatingEntities() { + $this->updateSetting('allow_duplicate', TRUE); + + $referenceNodes = $this->createReferenceContent(2); + $this->drupalCreateNode([ + 'type' => 'ief_test_complex', + 'title' => 'Some title', + 'multi' => array_values($referenceNodes), + ]); + /** @var \Drupal\node\NodeInterface $node */ + $parent_node = $this->drupalGetNodeByTitle('Some title'); + + $this->drupalGet('node/' . $parent_node->id() . '/edit'); + $this->drupalPostAjaxForm(NULL, [], $this->getButtonName('//input[@type="submit" and @id="edit-multi-entities-0-actions-ief-entity-duplicate"]')); + $this->assertResponse(200, 'Opening inline duplicate form was successful.'); + + $edit = [ + 'multi[form][inline_entity_form][entities][0][form][title][0][value]' => 'Duplicate!', + 'multi[form][inline_entity_form][entities][0][form][first_name][0][value]' => 'Bojan', + ]; + $this->drupalPostAjaxForm(NULL, $edit, $this->getButtonName('//input[@type="submit" and @data-drupal-selector="edit-multi-form-inline-entity-form-entities-0-form-actions-ief-duplicate-save"]')); + $this->assertResponse(200, 'Saving inline duplicate form was successful.'); + + $this->assertText('Some reference 1'); + $this->assertText('Some reference 2'); + $this->assertText('Duplicate!'); + $this->drupalPostForm(NULL, [], t('Save')); + $this->assertResponse(200, 'Saving parent entity was successful.'); + + // Confirm a duplicate was made. + $duplicate = Node::load(4); + $this->assertEqual($duplicate->label(), 'Duplicate!'); + $this->assertEqual($duplicate->first_name->value, 'Bojan'); + } + /** * Tests if a referenced content can be edited while the referenced content is * newer than the referencing parent node. */ public function testEditedInlineEntityValidation() { - $this->setAllowExisting(TRUE); + $this->updateSetting('allow_existing', TRUE); // Create referenced content. $referenced_nodes = $this->createReferenceContent(1); @@ -551,16 +587,18 @@ protected function createReferenceContent($numNodes = 3) { } /** - * Sets allow_existing IEF setting. + * Updates an IEF setting and saves the underlying entity display. * - * @param bool $flag - * "allow_existing" flag to be set. + * @param string $name + * The name of the setting. + * @param mixed $value + * The value to set. */ - protected function setAllowExisting($flag) { + protected function updateSetting($name, $value) { /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $display */ $display = $this->entityFormDisplayStorage->load('node.ief_test_complex.default'); $component = $display->getComponent('multi'); - $component['settings']['allow_existing'] = $flag; + $component['settings'][$name] = $value; $display->setComponent('multi', $component)->save(); } @@ -589,7 +627,7 @@ protected function createNodeForEveryBundle() { * Gets the form. * Opens the inline entity forms if they are not required. * - * @param boolean $required + * @param bool $required * Whether the fields are required. * @param array $permissions * (optional) Permissions to sign testing user in with. You may pass in an @@ -653,9 +691,9 @@ protected function openMultiExistingForm() { * Checks that an invalid value for an existing node will be display the expected error. * * @param $existing_node_text - * The text to enter into the existing node text field. + * The text to enter into the existing node text field. * @param $expected_error - * The error message that is expected to be shown. + * The error message that is expected to be shown. */ protected function checkExistingValidationExpectation($existing_node_text, $expected_error) { $edit = [ diff --git a/web/modules/inline_entity_form/src/Tests/ElementWebTest.php b/web/modules/inline_entity_form/src/Tests/ElementWebTest.php index 59a5a5f9f0..44686e884c 100644 --- a/web/modules/inline_entity_form/src/Tests/ElementWebTest.php +++ b/web/modules/inline_entity_form/src/Tests/ElementWebTest.php @@ -67,7 +67,7 @@ public function testCustomFormIEF() { $this->assertNodeByTitle($title, 'ief_test_custom'); if ($node = $this->getNodeByTitle($title)) { - $this->drupalGet("ief-edit-test/{$node->id()}/$form_mode_possibility"); + $this->drupalGet("ief-test/$form_mode_possibility/{$node->id()}"); $this->assertFieldByName('inline_entity_form[title][0][value]', $title, 'Node title appears in form.'); $this->checkFormDisplayFields("node.ief_test_custom.$form_mode_possibility", 'inline_entity_form'); $this->assertFieldByName('inline_entity_form[positive_int][0][value]', 11, 'Positive int field appears in form.'); diff --git a/web/modules/inline_entity_form/src/Tests/InlineEntityFormTestBase.php b/web/modules/inline_entity_form/src/Tests/InlineEntityFormTestBase.php index e3bf62d6da..d3a399f05e 100644 --- a/web/modules/inline_entity_form/src/Tests/InlineEntityFormTestBase.php +++ b/web/modules/inline_entity_form/src/Tests/InlineEntityFormTestBase.php @@ -140,7 +140,7 @@ protected function assertEntityByLabel($label, $entity_type_id = 'node', $bundle * in inline_entity_form_test module. * * @param $form_display - * The form display to check. + * The form display to check. */ protected function checkFormDisplayFields($form_display, $prefix) { $form_display_fields = [ @@ -171,7 +171,7 @@ protected function checkFormDisplayFields($form_display, $prefix) { ], ]; if ($fields = $form_display_fields[$form_display]) { - $this->assert('debug', 'Checking form dispaly: '. $form_display); + $this->assert('debug', 'Checking form dispaly: ' . $form_display); foreach ($fields['expected'] as $expected_field) { $this->assertFieldByName($prefix . $expected_field); } diff --git a/web/modules/inline_entity_form/src/Tests/SimpleWidgetWebTest.php b/web/modules/inline_entity_form/src/Tests/SimpleWidgetWebTest.php index 519c2611f8..531c602129 100644 --- a/web/modules/inline_entity_form/src/Tests/SimpleWidgetWebTest.php +++ b/web/modules/inline_entity_form/src/Tests/SimpleWidgetWebTest.php @@ -134,8 +134,8 @@ protected function testSimpleValidation() { $child_node = $this->getNodeByTitle($child_title); if ($this->assertNotNull($child_node)) { $this->assertEqual($host_node->single[0]->target_id, $child_node->id(), 'Child node is referenced'); - $this->assertEqual($child_node->positive_int[0]->value,1, 'Child node int field correct.'); - $this->assertEqual($child_node->bundle(),'ief_test_custom', 'Child node is correct bundle.'); + $this->assertEqual($child_node->positive_int[0]->value, 1, 'Child node int field correct.'); + $this->assertEqual($child_node->bundle(), 'ief_test_custom', 'Child node is correct bundle.'); } } } diff --git a/web/modules/inline_entity_form/src/Tests/TranslationTest.php b/web/modules/inline_entity_form/src/Tests/TranslationTest.php index 8d226a3bf3..b58949702b 100644 --- a/web/modules/inline_entity_form/src/Tests/TranslationTest.php +++ b/web/modules/inline_entity_form/src/Tests/TranslationTest.php @@ -56,7 +56,7 @@ protected function setUp() { /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $display */ $display = $form_display_storage->load('node.ief_test_complex.default'); $component = $display->getComponent('multi'); - $component['settings']['allow_existing'] = TRUe; + $component['settings']['allow_existing'] = TRUE; $display->setComponent('multi', $component)->save(); } diff --git a/web/modules/inline_entity_form/src/TranslationHelper.php b/web/modules/inline_entity_form/src/TranslationHelper.php index 6c71e5f054..6ff8670eb0 100644 --- a/web/modules/inline_entity_form/src/TranslationHelper.php +++ b/web/modules/inline_entity_form/src/TranslationHelper.php @@ -21,7 +21,7 @@ class TranslationHelper { * @return \Drupal\Core\Entity\ContentEntityInterface * The prepared entity. * - * @see \Drupal\Core\Entity\ContentEntityForm::initFormLangcodes(). + * @see \Drupal\Core\Entity\ContentEntityForm::initFormLangcodes() */ public static function prepareEntity(ContentEntityInterface $entity, FormStateInterface $form_state) { $form_langcode = $form_state->get('langcode'); @@ -34,6 +34,9 @@ public static function prepareEntity(ContentEntityInterface $entity, FormStateIn // Create a translation from the source language values. $source = $form_state->get(['content_translation', 'source']); $source_langcode = $source ? $source->getId() : $entity_langcode; + if (!$entity->hasTranslation($source_langcode)) { + $entity->addTranslation($source_langcode, $entity->toArray()); + } $source_translation = $entity->getTranslation($source_langcode); $entity->addTranslation($form_langcode, $source_translation->toArray()); $translation = $entity->getTranslation($form_langcode); @@ -97,7 +100,7 @@ public static function updateEntityLangcode(ContentEntityInterface $entity, $for * @return bool * TRUE if translating is in progress, FALSE otherwise. * - * @see \Drupal\Core\Entity\ContentEntityForm::initFormLangcodes(). + * @see \Drupal\Core\Entity\ContentEntityForm::initFormLangcodes() */ public static function isTranslating(FormStateInterface $form_state) { $form_langcode = $form_state->get('langcode'); diff --git a/web/modules/inline_entity_form/tests/modules/inline_entity_form_test/inline_entity_form_test.info.yml b/web/modules/inline_entity_form/tests/modules/inline_entity_form_test/inline_entity_form_test.info.yml index cc800ec7b7..699134915f 100644 --- a/web/modules/inline_entity_form/tests/modules/inline_entity_form_test/inline_entity_form_test.info.yml +++ b/web/modules/inline_entity_form/tests/modules/inline_entity_form_test/inline_entity_form_test.info.yml @@ -10,8 +10,8 @@ dependencies: - file - image -# Information added by Drupal.org packaging script on 2016-10-30 -version: '8.x-1.0-beta1' +# Information added by Drupal.org packaging script on 2018-05-22 +version: '8.x-1.0-rc1' core: '8.x' project: 'inline_entity_form' -datestamp: 1477868362 +datestamp: 1527030788 diff --git a/web/modules/inline_entity_form/tests/modules/inline_entity_form_test/inline_entity_form_test.routing.yml b/web/modules/inline_entity_form/tests/modules/inline_entity_form_test/inline_entity_form_test.routing.yml index aff490ae43..9cf5a05181 100644 --- a/web/modules/inline_entity_form/tests/modules/inline_entity_form_test/inline_entity_form_test.routing.yml +++ b/web/modules/inline_entity_form/tests/modules/inline_entity_form_test/inline_entity_form_test.routing.yml @@ -1,16 +1,9 @@ inline_entity_form_test.form: - path: '/ief-test/{form_mode}' + path: '/ief-test/{form_mode}/{node}' defaults: _form: '\Drupal\inline_entity_form_test\IefTest' _title: 'IEF test' form_mode: 'default' - requirements: - _access: 'TRUE' -inline_entity_form_test.edit_form: - path: '/ief-edit-test/{node}/{form_mode}' - defaults: - _form: '\Drupal\inline_entity_form_test\IefEditTest' - _title: 'IEF test' - form_mode: 'default' + node: NULL requirements: _access: 'TRUE' diff --git a/web/modules/inline_entity_form/tests/modules/inline_entity_form_test/src/IefEditTest.php b/web/modules/inline_entity_form/tests/modules/inline_entity_form_test/src/IefEditTest.php deleted file mode 100644 index 5437e85694..0000000000 --- a/web/modules/inline_entity_form/tests/modules/inline_entity_form_test/src/IefEditTest.php +++ /dev/null @@ -1,47 +0,0 @@ -<?php - -namespace Drupal\inline_entity_form_test; - -use Drupal\Core\Form\FormBase; -use Drupal\Core\Form\FormStateInterface; -use Drupal\node\Entity\Node; - -/** - * Tests Inline entity form element. - */ -class IefEditTest extends FormBase { - - /** - * {@inheritdoc} - */ - public function getFormID() { - return 'ief_edit_test'; - } - - /** - * {@inheritdoc} - */ - public function buildForm(array $form, FormStateInterface $form_state, Node $node = NULL, $form_mode = 'default') { - $form['inline_entity_form'] = [ - '#type' => 'inline_entity_form', - '#entity_type' => 'node', - '#bundle' => 'ief_test_custom', - '#default_value' => $node, - '#form_mode' => $form_mode, - ]; - $form['submit'] = [ - '#type' => 'submit', - '#value' => t('Update'), - ]; - return $form; - } - - /** - * {@inheritdoc} - */ - public function submitForm(array &$form, FormStateInterface $form_state) { - $entity = $form['inline_entity_form']['#entity']; - drupal_set_message(t('Created @entity_type @label.', ['@entity_type' => $entity->getEntityType()->getLabel(), '@label' => $entity->label()])); - } - -} diff --git a/web/modules/inline_entity_form/tests/modules/inline_entity_form_test/src/IefTest.php b/web/modules/inline_entity_form/tests/modules/inline_entity_form_test/src/IefTest.php index 2df5527023..344ee7c587 100644 --- a/web/modules/inline_entity_form/tests/modules/inline_entity_form_test/src/IefTest.php +++ b/web/modules/inline_entity_form/tests/modules/inline_entity_form_test/src/IefTest.php @@ -4,6 +4,7 @@ use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\node\Entity\Node; /** * Tests Inline entity form element. @@ -20,7 +21,7 @@ public function getFormID() { /** * {@inheritdoc} */ - public function buildForm(array $form, FormStateInterface $form_state, $form_mode = 'default') { + public function buildForm(array $form, FormStateInterface $form_state, $form_mode = 'default', Node $node = NULL) { $form['inline_entity_form'] = [ '#type' => 'inline_entity_form', '#entity_type' => 'node', @@ -31,6 +32,11 @@ public function buildForm(array $form, FormStateInterface $form_state, $form_mod '#type' => 'submit', '#value' => t('Save'), ]; + if (!empty($node)) { + $form['inline_entity_form']['#default_value'] = $node; + $form['submit']['#value'] = t('Update'); + } + return $form; } -- GitLab