diff --git a/config/schema/entity_browser.schema.yml b/config/schema/entity_browser.schema.yml index cf2c9ec..c38687a 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; + } + +}