| Conditions | 8 |
| Paths | 32 |
| Total Lines | 74 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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('Embed'), |
||
| 137 | '#button_type' => 'primary', |
||
| 138 | // No regular submit-handler. This form only works via JavaScript. |
||
| 139 | '#submit' => array(), |
||
| 140 | '#ajax' => array( |
||
| 141 | 'callback' => '::submitForm', |
||
| 142 | 'event' => 'click', |
||
| 143 | ), |
||
| 144 | ); |
||
| 145 | |||
| 146 | return $form; |
||
| 147 | } |
||
| 148 | |||
| 173 |