From 6503a6dcea48eb7306ca40f63fb5c08aafc6a4e2 Mon Sep 17 00:00:00 2001 From: yannickoo Date: Mon, 13 Jun 2016 17:00:19 +0200 Subject: [PATCH] Issue #2513086 by JamesK: Right click option to edit entity: not reference a different entity, but *edit* the entity in a dialog --- src/Form/EntityEmbedDialog.php | 50 ++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/src/Form/EntityEmbedDialog.php b/src/Form/EntityEmbedDialog.php index 6ca9e795..e06e658c 100644 --- a/src/Form/EntityEmbedDialog.php +++ b/src/Form/EntityEmbedDialog.php @@ -15,6 +15,7 @@ use Drupal\Core\Ajax\SetDialogTitleCommand; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormBuilderInterface; use Drupal\Core\Form\FormStateInterface; @@ -56,6 +57,13 @@ class EntityEmbedDialog extends FormBase { */ protected $eventDispatcher; + /** + * The module handler. + * + * @var \Drupal\Core\Extension\ModuleHandlerInterface + */ + protected $moduleHandler; + /** * The entity browser. * @@ -79,12 +87,15 @@ class EntityEmbedDialog extends FormBase { * The entity type manager service. * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher * Event dispatcher service. + * @param \Drupal\Core\Extension\ModuleHandlerInterface + * The module handler. */ - public function __construct(EntityEmbedDisplayManager $plugin_manager, FormBuilderInterface $form_builder, EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher) { + public function __construct(EntityEmbedDisplayManager $plugin_manager, FormBuilderInterface $form_builder, EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher, ModuleHandlerInterface $module_handler) { $this->setDisplayPluginManager($plugin_manager); $this->formBuilder = $form_builder; $this->entityTypeManager = $entity_type_manager; $this->eventDispatcher = $event_dispatcher; + $this->moduleHandler = $module_handler; } /** @@ -95,7 +106,8 @@ public static function create(ContainerInterface $container) { $container->get('plugin.manager.entity_embed.display'), $container->get('form_builder'), $container->get('entity_type.manager'), - $container->get('event_dispatcher') + $container->get('event_dispatcher'), + $container->get('module_handler') ); } @@ -361,6 +373,40 @@ public function buildEmbedStep(array $form, FormStateInterface $form_state) { '#title' => $this->t('Selected entity'), '#markup' => $entity_label, ); + + $edit_url = $entity->urlInfo('edit-form'); + $form['entity_edit'] = [ + '#type' => 'link', + '#title' => $this->t('Edit'), + '#url' => $edit_url, + '#attributes' => [ + 'target' => '_blank', + 'class' => ['button'], + ], + ]; + + if ($this->moduleHandler->moduleExists('entity_browser')) { + // Configuration entities have no form object so we provide a fallback + // to a normal link styled like a button. + try { + $edit_form = $this->entityManager()->getFormObject($entity->getEntityTypeId(), 'edit'); + + $form['entity_edit'] = [ + '#type' => 'button', + '#executes_submit_callback' => FALSE, + '#value' => $this->t('Edit'), + '#ajax' => [ + 'url' => \Drupal\Core\Url::fromRoute( + 'entity_browser.edit_form', [ + 'entity_type' => $entity->getEntityTypeId(), + 'entity' => $entity->id(), + ] + ) + ], + ]; + } catch (\Exception $e) {} + } + $form['attributes']['data-entity-type'] = array( '#type' => 'hidden', '#value' => $entity_element['data-entity-type'],