Completed
Push — 8.x-1.x ( f45fe8...755b68 )
by
unknown
04:06
created

UrlEmbedDialog::buildForm()   C

Complexity

Conditions 8
Paths 32

Size

Total Lines 73
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 73
rs 6.3385
cc 8
eloc 47
nc 32
nop 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @file
5
 * Contains \Drupal\url_embed\Form\UrlEmbedDialog.
6
 */
7
8
namespace Drupal\url_embed\Form;
9
10
use Drupal\Core\Ajax\AjaxResponse;
11
use Drupal\Core\Ajax\CloseModalDialogCommand;
12
use Drupal\Core\Ajax\HtmlCommand;
13
use Drupal\Core\Form\FormBase;
14
use Drupal\Core\Form\FormBuilderInterface;
15
use Drupal\Core\Form\FormStateInterface;
16
use Drupal\editor\Ajax\EditorDialogSave;
17
use Drupal\editor\EditorInterface;
18
use Drupal\embed\EmbedButtonInterface;
19
use Drupal\url_embed\UrlEmbedHelperTrait;
20
use Drupal\url_embed\UrlEmbedInterface;
21
use Symfony\Component\DependencyInjection\ContainerInterface;
22
23
/**
24
 * Provides a form to embed URLs.
25
 */
26
class UrlEmbedDialog extends FormBase {
27
  use UrlEmbedHelperTrait;
28
29
  /**
30
   * The form builder.
31
   *
32
   * @var \Drupal\Core\Form\FormBuilderInterface
33
   */
34
  protected $formBuilder;
35
36
  /**
37
   * Constructs a UrlEmbedDialog object.
38
   *
39
   * @param \Drupal\url_embed\UrlEmbedInterface $url_embed
40
   *   The URL embed service.
41
   * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
42
   *   The Form Builder.
43
   */
44
  public function __construct(UrlEmbedInterface $url_embed, FormBuilderInterface $form_builder) {
45
    $this->setUrlEmbed($url_embed);
46
    $this->formBuilder = $form_builder;
47
  }
48
49
  /**
50
   * {@inheritdoc}
51
   */
52
  public static function create(ContainerInterface $container) {
53
    return new static(
54
      $container->get('url_embed'),
55
      $container->get('form_builder')
56
    );
57
  }
58
59
  /**
60
   * {@inheritdoc}
61
   */
62
  public function getFormId() {
63
    return 'url_embed_dialog';
64
  }
65
66
  /**
67
   * {@inheritdoc}
68
   *
69
   * @param \Drupal\editor\EditorInterface $editor
70
   *   The editor to which this dialog corresponds.
71
   * @param \Drupal\embed\EmbedButtonInterface $embed_button
72
   *   The URL button to which this dialog corresponds.
73
   */
74
  public function buildForm(array $form, FormStateInterface $form_state, EditorInterface $editor = NULL, EmbedButtonInterface $embed_button = NULL) {
75
    $values = $form_state->getValues();
76
    $input = $form_state->getUserInput();
77
    // Set URL button element in form state, so that it can be used later in
78
    // validateForm() function.
79
    $form_state->set('embed_button', $embed_button);
80
    $form_state->set('editor', $editor);
81
    // Initialize URL element with form attributes, if present.
82
    $url_element = empty($values['attributes']) ? array() : $values['attributes'];
83
    $url_element += empty($input['attributes']) ? array() : $input['attributes'];
84
    // The default values are set directly from \Drupal::request()->request,
85
    // provided by the editor plugin opening the dialog.
86
    if (!$form_state->get('url_element')) {
87
      $form_state->set('url_element', isset($input['editor_object']) ? $input['editor_object'] : array());
88
    }
89
    $url_element += $form_state->get('url_element');
90
    $url_element += array(
91
      'data-embed-url' => '',
92
      'data-url-provider' => '',
93
    );
94
    $form_state->set('url_element', $url_element);
95
96
    $form['#tree'] = TRUE;
97
    $form['#attached']['library'][] = 'editor/drupal.editor.dialog';
98
    $form['#prefix'] = '<div id="url-embed-dialog-form">';
99
    $form['#suffix'] = '</div>';
100
101
    $form['attributes']['data-embed-url'] = array(
102
      '#type' => 'textfield',
103
      '#title' => 'URL',
104
      '#default_value' => $url_element['data-embed-url'],
105
      '#required' => TRUE,
106
    );
107
108
    try {
109
      if (!empty($url_element['data-embed-url']) && $info = $this->urlEmbed()->getEmbed($url_element['data-embed-url'])) {
110
        $url_element['data-url-provider'] = $info->getProviderName();
111
      }
112
    }
113
    catch (\Exception $e) {
114
      watchdog_exception('url_embed', $e);
115
    }
116
117
    $form['attributes']['data-url-provider'] = array(
118
      '#type' => 'value',
119
      '#value' => $url_element['data-url-provider'],
120
    );
121
122
    $form['attributes']['data-embed-button'] = array(
123
      '#type' => 'value',
124
      '#value' => $embed_button->id(),
125
    );
126
    $form['attributes']['data-entity-label'] = array(
127
      '#type' => 'value',
128
      '#value' => $embed_button->label(),
129
    );
130
131
    $form['actions'] = array(
132
      '#type' => 'actions',
133
    );
134
    $form['actions']['save_modal'] = array(
135
      '#type' => 'submit',
136
      '#value' => $this->t('Save'),
137
      // No regular submit-handler. This form only works via JavaScript.
138
      '#submit' => array(),
139
      '#ajax' => array(
140
        'callback' => '::submitForm',
141
        'event' => 'click',
142
      ),
143
    );
144
145
    return $form;
146
  }
147
148
  /**
149
   * {@inheritdoc}
150
   */
151
  public function submitForm(array &$form, FormStateInterface $form_state) {
152
    $response = new AjaxResponse();
153
154
    $values = $form_state->getValues();
155
    // Display errors in form, if any.
156
    if ($form_state->hasAnyErrors()) {
157
      unset($form['#prefix'], $form['#suffix']);
158
      $form['status_messages'] = array(
159
        '#type' => 'status_messages',
160
        '#weight' => -10,
161
      );
162
      $response->addCommand(new HtmlCommand('#url-embed-dialog-form', $form));
163
    }
164
    else {
165
      $response->addCommand(new EditorDialogSave($values));
166
      $response->addCommand(new CloseModalDialogCommand());
167
    }
168
169
    return $response;
170
  }
171
}
172