Completed
Push — 8.x-1.x ( 7f5383...eb39b1 )
by Janez
03:40
created

EntityBrowserController   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 2
c 1
b 0
f 1
lcom 0
cbo 0
dl 0
loc 37
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A entityBrowserEdit() 0 23 2
1
<?php
2
/**
3
 * @file
4
 * Contains \Drupal\entity_browser\Controllers\EntityBrowserController.
5
 */
6
7
namespace Drupal\entity_browser\Controllers;
8
9
use Drupal\Core\Ajax\AjaxResponse;
10
use Drupal\Core\Ajax\CloseModalDialogCommand;
11
use Drupal\Core\Ajax\OpenModalDialogCommand;
12
use Drupal\Core\Controller\ControllerBase;
13
use Drupal\Core\Form\FormState;
14
use Drupal\Core\Entity\EntityInterface;
15
16
/**
17
 * Returns responses for entity browser routes.
18
 */
19
class EntityBrowserController extends ControllerBase {
20
21
  /**
22
   * Return an Ajax dialog command for editing a referenced entity.
23
   *
24
   * @param \Drupal\Core\Entity\EntityInterface $entity
25
   *   An entity being edited.
26
   *
27
   * @return \Drupal\Core\Ajax\AjaxResponse
28
   *   An Ajax response with a command for opening or closing the dialog
29
   *   containing the edit form.
30
   */
31
  public function entityBrowserEdit(EntityInterface $entity) {
32
    // Build the entity edit form.
33
    $form_object = $this->entityManager()->getFormObject($entity->getEntityTypeId(), 'edit');
34
    $form_object->setEntity($entity);
35
    $form_state = (new FormState())
36
      ->setFormObject($form_object)
37
      ->disableRedirect();
38
    // Building the form also submits.
39
    $form = $this->formBuilder()->buildForm($form_object, $form_state);
40
41
    // Return a response, depending on whether it's successfully submitted.
42
    if (!$form_state->isExecuted()) {
43
      // Return the form as a modal dialog.
44
      $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
45
      $title = $this->t('Edit entity @entity', ['@entity' => $entity->label()]);
46
      $response = AjaxResponse::create()->addCommand(new OpenModalDialogCommand($title, $form, ['width' => 800]));
47
      return $response;
48
    }
49
    else {
50
      // Return command for closing the modal.
51
      return AjaxResponse::create()->addCommand(new CloseModalDialogCommand());
52
    }
53
  }
54
55
}
56