editor_advanced_link.info.yml | 4 +++- editor_advanced_link.module | 36 ++++++++++++++++++++++-------------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/editor_advanced_link.info.yml b/editor_advanced_link.info.yml index b17c960..026a7f7 100644 --- a/editor_advanced_link.info.yml +++ b/editor_advanced_link.info.yml @@ -1,5 +1,7 @@ name: 'Advanced Link' -description: 'Add a title and a target fields on editor''s link dialog if the filter allows it.' +description: 'Add title, target etc. attributes to Text Editor''s link dialog if the text format allows them.' type: module core: 8.x version: VERSION +dependencies: + - editor diff --git a/editor_advanced_link.module b/editor_advanced_link.module index 86f6d5c..58ddf38 100644 --- a/editor_advanced_link.module +++ b/editor_advanced_link.module @@ -15,20 +15,28 @@ use Drupal\Core\Form\FormStateInterface; * going to be called as an implementation of hook_form_BASE_FORM_ID_alter(). */ function editor_advanced_link_form_editor_link_dialog_alter(&$form, FormStateInterface $form_state, $form_id) { + /** @var \Drupal\filter\FilterFormatInterface $filter_format */ $filter_format = $form_state->getBuildInfo()['args'][0]; $restrictions = $filter_format->getHtmlRestrictions(); $user_input = $form_state->getUserInput(); + $get_default_value = function($attribute_name, $fallback = '') use ($user_input) { + return !empty($user_input['editor_object'][$attribute_name]) ? $user_input['editor_object'][$attribute_name] : $fallback; + }; + $is_accessible = function($attribute_name) use ($restrictions) { + return $restrictions === FALSE || $restrictions['allowed']['a'] === TRUE || $restrictions['allowed']['a'][$attribute_name] === TRUE; + }; + $form['attributes']['href']['#weight'] = 0; $form['attributes']['title'] = [ '#type' => 'textfield', '#title' => t('Title'), '#description' => t('Populates the title attribute of the link, usually shown as a small tooltip on hover.'), - '#default_value' => !empty($user_input['editor_object']['title']) ? $user_input['editor_object']['title'] : '', + '#default_value' => $get_default_value('title'), '#maxlength' => 512, '#weight' => 1, - '#access' => $restrictions === FALSE || $restrictions['allowed']['a'] === TRUE || $restrictions['allowed']['a']['title'] === TRUE, + '#access' => $is_accessible('title'), ]; $form['advanced'] = [ @@ -41,45 +49,45 @@ function editor_advanced_link_form_editor_link_dialog_alter(&$form, FormStateInt $form['attributes']['class'] = [ '#type' => 'textfield', '#title' => t('CSS classes'), - '#description' => t('List of CSS classes to add to the link, separated by a space.'), - '#default_value' => !empty($user_input['editor_object']['class']) ? $user_input['editor_object']['class'] : '', + '#description' => t('List of CSS classes to add to the link, separated by spaces.'), + '#default_value' => $get_default_value('class'), '#maxlength' => 512, - '#access' => $restrictions === FALSE || $restrictions['allowed']['a'] === TRUE || $restrictions['allowed']['a']['class'] === TRUE, + '#access' => $is_accessible('class'), '#group' => 'advanced', ]; $form['attributes']['id'] = [ '#type' => 'textfield', '#title' => t('ID'), - '#description' => t('This ID have to be unique. Be careful of the already used IDs in your pages.'), - '#default_value' => !empty($user_input['editor_object']['id']) ? $user_input['editor_object']['id'] : '', + '#description' => t('Allows linking to this content using a URL fragment). Must be unique.'), + '#default_value' => $get_default_value('id'), '#maxlength' => 512, - '#access' => $restrictions === FALSE || $restrictions['allowed']['a'] === TRUE || $restrictions['allowed']['a']['id'] === TRUE, + '#access' => $is_accessible('id'), '#group' => 'advanced', ]; $form['attributes']['target'] = [ '#title' => t('Open in new window'), '#type' => 'checkbox', - '#default_value' => !empty($user_input['editor_object']['target']) ? $user_input['editor_object']['target'] : FALSE, + '#default_value' => $get_default_value('target', FALSE), '#return_value' => '_blank', '#element_validate' => ['_editor_advanced_link_target_validate'], - '#access' => $restrictions === FALSE || $restrictions['allowed']['a'] === TRUE || $restrictions['allowed']['a']['target'] === TRUE, + '#access' => $is_accessible('target'), '#group' => 'advanced', ]; $form['attributes']['rel'] = [ '#type' => 'textfield', '#title' => t('Relation (rel)'), - '#description' => t('Content of the "rel" attribute. Often used by JS galleries.'), - '#default_value' => !empty($user_input['editor_object']['rel']) ? $user_input['editor_object']['rel'] : '', + '#description' => t('Often used by JS galleries.'), + '#default_value' => $get_default_value('rel'), '#maxlength' => 512, - '#access' => $restrictions === FALSE || $restrictions['allowed']['a'] === TRUE || $restrictions['allowed']['a']['rel'] === TRUE, + '#access' => $is_accessible('rel'), '#group' => 'advanced', ]; foreach ($form['attributes'] as $element) { - if (!empty($element['#group']) && $element['#group'] == 'advanced' && !empty($element['#access'])) { + if ($element['#group'] === 'advanced' && $element['#access'] === TRUE) { $form['advanced']['#access'] = TRUE; break; }