diff --git a/src/Plugin/Field/FieldWidget/InlineEntityFormBase.php b/src/Plugin/Field/FieldWidget/InlineEntityFormBase.php index 43e173a..da6a1ab 100644 --- a/src/Plugin/Field/FieldWidget/InlineEntityFormBase.php +++ b/src/Plugin/Field/FieldWidget/InlineEntityFormBase.php @@ -315,7 +315,6 @@ abstract class InlineEntityFormBase extends WidgetBase implements ContainerFacto * Form array structure. */ public static function addIefSubmitCallbacks($element) { - $element['#element_validate'][] = [get_called_class(), 'validateSaveEntity']; $element['#ief_element_submit'][] = [get_called_class(), 'submitSaveEntity']; return $element; } @@ -366,7 +365,7 @@ abstract class InlineEntityFormBase extends WidgetBase implements ContainerFacto * @param $form_state * The form state of the parent form. */ - public static function validateSaveEntity($entity_form, FormStateInterface $form_state) { + public static function submitSaveEntity($entity_form, FormStateInterface $form_state) { $ief_id = $entity_form['#ief_id']; /** @var \Drupal\Core\Entity\EntityInterface $entity */ $entity = $entity_form['#entity']; @@ -383,7 +382,7 @@ abstract class InlineEntityFormBase extends WidgetBase implements ContainerFacto 'entity' => $entity, '_weight' => $weight, 'form' => NULL, - 'needs_save' => FALSE, + 'needs_save' => TRUE, ); $form_state->set(['inline_entity_form', $ief_id, 'entities'], $entities); } @@ -391,27 +390,6 @@ abstract class InlineEntityFormBase extends WidgetBase implements ContainerFacto $delta = $entity_form['#ief_row_delta']; $entities = $form_state->get(['inline_entity_form', $ief_id, 'entities']); $entities[$delta]['entity'] = $entity; - $entities[$delta]['needs_save'] = FALSE; - $form_state->set(['inline_entity_form', $ief_id, 'entities'], $entities); - } - } - - public static function submitSaveEntity($entity_form, FormStateInterface $form_state) { - $ief_id = $entity_form['#ief_id']; - $entity = $entity_form['#entity']; - - if ($entity_form['#op'] == 'add') { - $entities = $form_state->get(['inline_entity_form', $ief_id, 'entities']); - $item = array_pop($entities); - $item['entity'] = $entity; - $item['needs_save'] = TRUE; - array_push($entities, $item); - $form_state->set(['inline_entity_form', $ief_id, 'entities'], $entities); - } - else { - $delta = $entity_form['#ief_row_delta']; - $entities = $form_state->get(['inline_entity_form', $ief_id, 'entities']); - $entities[$delta]['entity'] = $entity; $entities[$delta]['needs_save'] = TRUE; $form_state->set(['inline_entity_form', $ief_id, 'entities'], $entities); } diff --git a/src/Plugin/Field/FieldWidget/InlineEntityFormSimple.php b/src/Plugin/Field/FieldWidget/InlineEntityFormSimple.php index 553605c..102e734 100644 --- a/src/Plugin/Field/FieldWidget/InlineEntityFormSimple.php +++ b/src/Plugin/Field/FieldWidget/InlineEntityFormSimple.php @@ -100,7 +100,9 @@ class InlineEntityFormSimple extends InlineEntityFormBase { return; } - $values[$submitted_values[$delta]['_weight']] = ['entity' => $entity]; + $weight = isset($submitted_values[$delta]['_weight']) ? $submitted_values[$delta]['_weight'] : 0; + + $values[$weight] = ['entity' => $entity]; } // Sort items base on weights. diff --git a/src/Tests/InlineEntityFormWebTest.php b/src/Tests/InlineEntityFormWebTest.php index 050e838..ee449c4 100644 --- a/src/Tests/InlineEntityFormWebTest.php +++ b/src/Tests/InlineEntityFormWebTest.php @@ -7,6 +7,7 @@ namespace Drupal\inline_entity_form\Tests; +use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\simpletest\WebTestBase; /** @@ -29,6 +30,14 @@ class InlineEntityFormWebTest extends WebTestBase { * @var \Drupal\user\Entity\User */ protected $user; + + /** + * Field config storage. + * + * @var \Drupal\Core\Config\Entity\ConfigEntityStorage + */ + protected $fieldStorageConfigStorage; + /** * Prepares environment for */ @@ -41,6 +50,10 @@ class InlineEntityFormWebTest extends WebTestBase { 'edit any ief_test_custom content', 'view own unpublished content', ]); + + $this->fieldStorageConfigStorage = $this->container + ->get('entity_type.manager') + ->getStorage('field_storage_config'); } /** @@ -65,17 +78,57 @@ class InlineEntityFormWebTest extends WebTestBase { * Tests simple IEF widget with single-value field. */ public function testSimpleSingle() { - $this->drupalLogin($this->user); - $this->drupalGet('node/add/ief_simple_single'); + $cardinality_options = [ + 1 => 1, + 2 => 2, + FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED => 1, + ]; + foreach ($cardinality_options as $cardinality => $limit) { + /** @var \Drupal\field\FieldStorageConfigInterface $field_storage */ + $field_storage = $this->fieldStorageConfigStorage->load('node.single'); + $field_storage->setCardinality($cardinality); + $field_storage->save(); - $this->assertText('Single node', 'Inline entity field widget title found.'); - $this->assertText('Reference a single node.', 'Inline entity field description found.'); + $this->drupalLogin($this->user); + $this->drupalGet('node/add/ief_simple_single'); - $edit = [ - 'title[0][value]' => 'Host node', - 'single[0][inline_entity_form][title][0][value]' => 'Child node', - ]; - $this->drupalPostForm('node/add/ief_simple_single', $edit, t('Save')); + $this->assertText('Single node', 'Inline entity field widget title found.'); + $this->assertText('Reference a single node.', 'Inline entity field description found.'); + + $edit = ['title[0][value]' => 'Host node']; + for ($item_number = 0; $item_number < $limit; $item_number++) { + $edit["single[$item_number][inline_entity_form][title][0][value]"] = 'Child node nr.' . $item_number; + } + + $this->drupalPostForm('node/add/ief_simple_single', $edit, t('Save')); + + for ($item_number = 0; $item_number < $limit; $item_number++) { + $this->assertText('Child node nr.' . $item_number, 'Label of referenced entity found.'); + } + } + } + + /** + * Gets IEF button name. + * + * @param array $xpath + * Xpath of the button. + * + * @return string + * The name of the button. + */ + protected function getButtonName($xpath) { + $retval = ''; + /** @var \SimpleXMLElement[] $elements */ + if ($elements = $this->xpath($xpath)) { + foreach ($elements[0]->attributes() as $name => $value) { + if ($name == 'name') { + $retval = $value; + break; + } + } + } + return $retval; } } diff --git a/tests/modules/inline_entity_form_test/config/install/core.entity_view_display.node.ief_simple_single.default.yml b/tests/modules/inline_entity_form_test/config/install/core.entity_view_display.node.ief_simple_single.default.yml new file mode 100644 index 0000000..0b60ebf --- /dev/null +++ b/tests/modules/inline_entity_form_test/config/install/core.entity_view_display.node.ief_simple_single.default.yml @@ -0,0 +1,22 @@ +langcode: en +status: true +dependencies: + config: + - field.field.node.ief_simple_single.single + - node.type.ief_simple_single + module: + - user +id: node.ief_simple_single.default +targetEntityType: node +bundle: ief_simple_single +mode: default +content: + single: + type: entity_reference_label + weight: 0 + label: above + settings: + link: false + third_party_settings: { } +hidden: + links: true diff --git a/tests/modules/inline_entity_form_test/config/install/node.type.ief_test_custom.yml b/tests/modules/inline_entity_form_test/config/install/node.type.ief_test_custom.yml index c1808a9..ccf0bc7 100644 --- a/tests/modules/inline_entity_form_test/config/install/node.type.ief_test_custom.yml +++ b/tests/modules/inline_entity_form_test/config/install/node.type.ief_test_custom.yml @@ -1,5 +1,5 @@ type: ief_test_custom -name: IEF test cestim +name: IEF test custom description: 'Content type for IEF custom form testing.' help: '' new_revision: false