Skip to content
This repository has been archived by the owner on Jan 5, 2018. It is now read-only.

Commit

Permalink
Issue #2513086 by JamesK: Right click option to edit entity: not refe…
Browse files Browse the repository at this point in the history
…rence a different entity, but *edit* the entity in a dialog
  • Loading branch information
yannickoo committed Jun 13, 2016
1 parent 607254d commit 6503a6d
Showing 1 changed file with 48 additions and 2 deletions.
50 changes: 48 additions & 2 deletions src/Form/EntityEmbedDialog.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,6 +57,13 @@ class EntityEmbedDialog extends FormBase {
*/
protected $eventDispatcher;

/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;

/**
* The entity browser.
*
Expand All @@ -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;
}

/**
Expand All @@ -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')
);
}

Expand Down Expand Up @@ -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'],
Expand Down

0 comments on commit 6503a6d

Please sign in to comment.