diff --git a/config/schema/entity_browser.schema.yml b/config/schema/entity_browser.schema.yml index 84ff907..72e8b2a 100644 --- a/config/schema/entity_browser.schema.yml +++ b/config/schema/entity_browser.schema.yml @@ -201,6 +201,14 @@ entity_browser.field_widget_display.rendered_entity: type: string label: 'View mode' +entity_browser.field_widget_display.inline_entity_form: + type: mapping + label: 'Inline entity form display widget' + mapping: + form_mode: + type: string + label: 'Form mode' + field.widget.settings.entity_browser_file: type: mapping label: 'Entity browser file widget' diff --git a/modules/entity_form/src/Plugin/EntityBrowser/FieldWidgetDisplay/InlineEntityForm.php b/modules/entity_form/src/Plugin/EntityBrowser/FieldWidgetDisplay/InlineEntityForm.php new file mode 100644 index 0000000..f192bf9 --- /dev/null +++ b/modules/entity_form/src/Plugin/EntityBrowser/FieldWidgetDisplay/InlineEntityForm.php @@ -0,0 +1,144 @@ +entityTypeManager = $entity_type_manager; + $this->entityDisplayRepository = $entity_display_repository; + $this->user = $user; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('entity_type.manager'), + $container->get('entity_display.repository'), + $container->get('current_user') + ); + } + + /** + * {@inheritdoc} + */ + public function view(EntityInterface $entity) { + if ($entity->access('update', $this->user)) { + return [ + '#type' => 'inline_entity_form', + '#entity_type' => $entity->getEntityTypeId(), + '#bundle' => $entity->bundle(), + '#default_value' => $entity, + '#form_mode' => $this->configuration['form_mode'], + ]; + } + else { + return $entity->label(); + } + } + + /** + * {@inheritdoc} + */ + public function settingsForm(array $form, FormStateInterface $form_state) { + $options = parent::settingsForm($form, $form_state); + + foreach ($this->entityDisplayRepository->getFormModeOptions($this->configuration['entity_type']) as $id => $form_mode_label) { + $options[$id] = $form_mode_label; + } + + return [ + 'form_mode' => [ + '#type' => 'select', + '#title' => $this->t('Form mode'), + '#description' => $this->t('Select form mode to be used when rendering entities.'), + '#default_value' => $this->configuration['form_mode'], + '#options' => $options, + ], + ]; + } + + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + return [ + 'form_mode' => 'default', + ] + parent::defaultConfiguration(); + } + + /** + * {@inheritdoc} + */ + public function calculateDependencies() { + $dependencies = parent::calculateDependencies(); + + if ($form_mode = $this->entityTypeManager->getStorage('entity_form_mode')->load($this->configuration['entity_type'] . '.' . $this->configuration['form_mode'])) { + $dependencies[$form_mode->getConfigDependencyKey()][] = $form_mode->getConfigDependencyName(); + } + + return $dependencies; + } + +} diff --git a/modules/entity_form/tests/src/FunctionalJavascript/InlineEntityFormFieldWidgetDisplayTest.php b/modules/entity_form/tests/src/FunctionalJavascript/InlineEntityFormFieldWidgetDisplayTest.php new file mode 100644 index 0000000..36a1ce3 --- /dev/null +++ b/modules/entity_form/tests/src/FunctionalJavascript/InlineEntityFormFieldWidgetDisplayTest.php @@ -0,0 +1,152 @@ +drupalCreateContentType([ + 'type' => 'foo', + 'name' => 'Foo', + ]); + + FieldStorageConfig::create([ + 'field_name' => 'field_reference', + 'type' => 'entity_reference', + 'entity_type' => 'node', + 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, + 'settings' => [ + 'target_type' => 'node', + ], + ])->save(); + + FieldConfig::create([ + 'field_name' => 'field_reference', + 'entity_type' => 'node', + 'bundle' => 'foo', + 'label' => 'Reference', + 'settings' => [], + ])->save(); + + /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */ + $form_display = $this->container->get('entity_type.manager') + ->getStorage('entity_form_display') + ->load('node.foo.default'); + + $form_display->setComponent('field_reference', [ + 'type' => 'entity_browser_entity_reference', + 'settings' => [ + 'entity_browser' => 'entity_browser_test_entity_form', + 'field_widget_display' => 'inline_entity_form', + 'field_widget_display_settings' => [ + 'form_mode' => 'default', + ], + 'open' => TRUE, + ], + ])->save(); + + /** @var \Drupal\Core\Entity\Display\EntityDisplayInterface $display */ + $display = $this->container->get('entity_type.manager') + ->getStorage('entity_view_display') + ->load('node.foo.default'); + + $display->setComponent('field_reference', [ + 'settings' => [ + 'link' => TRUE, + ], + 'type' => 'entity_reference_label', + 'region' => 'content', + ])->save(); + + $account = $this->drupalCreateUser(static::$userPermissions); + $this->drupalLogin($account); + } + + /** + * Testing of inline entity form field widget. + */ + public function testInlineEntityFormWidget() { + $this->drupalGet('node/add/foo'); + $page = $this->getSession()->getPage(); + + // Fill form and create new entity inside entity browser. + $page->fillField('title[0][value]', 'Cartoon quotes'); + $page->clickLink('Select entities'); + $this->getSession() + ->switchToIFrame('entity_browser_iframe_entity_browser_test_entity_form'); + $page->fillField('inline_entity_form[title][0][value]', 'Trees and people used to be good friends'); + $page->pressButton('Save entity'); + + // Switch back to the main form. + $this->getSession()->switchToIFrame(); + $this->assertSession()->assertWaitOnAjaxRequest(); + + // Check that form for inner entity exists and that field is filled. + $this->assertSession() + ->fieldValueEquals('field_reference[current][items][0][display][title][0][value]', 'Trees and people used to be good friends'); + $page->pressButton('Save'); + + // Check view display for entity. + $this->drupalGet('/node/2'); + $this->assertSession() + ->pageTextContains('Trees and people used to be good friends'); + + // Edit entity with changing value in inline form and check that value is + // correct after entity is saved. + $this->drupalGet('/node/2/edit'); + $page->fillField('field_reference[current][items][0][display][title][0][value]', 'Trees and people used to be good friends [Tatsuo Kusakabe]'); + $page->pressButton('Save'); + + $this->assertSession() + ->pageTextContains('Trees and people used to be good friends [Tatsuo Kusakabe]'); + } + +}