Problem/Motivation

There are several modules allowing users to open links in a new window:

All modules use a different way of allowing the users to do this:

  • This module uses a textfield
  • menu_link_attributes uses a select field
  • editor_advanced_link uses a checkbox

Proposed resolution

It would be nice to provide editors with the same options everywhere. I think the checkbox is the easiest/ most user friendly, but if you want to have several options I guess the select field is the most flexible solution. So let's convert the textfield to a select box.

Remaining tasks

Change the text field to a select field.
Discuss what to do with existing widgets/data.

User interface changes

The textfield will be converted to a select field.

API changes

-

Data model changes

-

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

seanB created an issue. See original summary.

seanB’s picture

Title: Target as dropdown » Target option as select field
seanB’s picture

larowlan’s picture

I'd support this if it could be done in a way that didn't introduce a switch or else statement.

So it would probably require moving the attributes to plugins.

seanB’s picture

+1 on moving attributes to plugins. I'll try to create a patch for that asap.

larowlan’s picture

Ideally we could do this without changing the data structure.

So that would require the plugin IDs matching the attribute names.

Thanks

larowlan’s picture

FWIW I would support yaml based discovery for this

A base class would handle most cases.

seanB’s picture

Status: Active » Needs review
FileSize
5.52 KB

Ok, this should do it. Decided to remove the field descriptions, they didn't seem to add any value. I could add them back if you want, but it seems better this way.

The data structure is still intact, so besides the field description and the select for the target nobody should notice a thing. Changing the target to a select means that manually entered values other then _self or _blank could cause an issue. Links that used to open in a new window will return to the default value _self after saving the content. Not sure what to do with those?

Status: Needs review » Needs work

The last submitted patch, 8: 2893078-8.patch, failed testing. View results

seanB’s picture

Status: Needs work » Needs review
FileSize
5.5 KB
981 bytes

Options should not be a default property. Also fixed leftover docs from drupal console.

larowlan’s picture

Issue tags: +needs documentation updates

This is looking great, really simple.

Adding tag to update documentation.

  1. +++ b/src/LinkAttributesManager.php
    @@ -0,0 +1,67 @@
    +  protected $defaults = array(
    +    'title' => '',
    +    'type' => '',
    +    'description' => '',
    +  );
    

    can we short syntax here, thanks

  2. +++ b/src/LinkAttributesManager.php
    @@ -0,0 +1,67 @@
    +    // Add more services as required.
    

    Please remove this - console cruft?

  3. +++ b/src/LinkAttributesManager.php
    @@ -0,0 +1,67 @@
    +    // Make sure each plugin definition had at least a field type.
    +    if (empty($definition['type'])) {
    +      throw new PluginException(sprintf('Link attribute plugin property (%s) definition is required.', $plugin_id));
    +    }
    

    If its missing, lets default to 'textfield' and then remove the 'textfield' from the yml file, so we only have to nominate type if its out of the ordinary, less verbose

  4. +++ b/src/Plugin/Field/FieldWidget/LinkWithAttributesWidget.php
    @@ -55,13 +55,15 @@ class LinkWithAttributesWidget extends LinkWidget {
    +    $plugin_definitions = \Drupal::service('plugin.manager.link_attributes')->getDefinitions();
    
    @@ -71,12 +73,16 @@ class LinkWithAttributesWidget extends LinkWidget {
    +    $plugin_definitions = \Drupal::service('plugin.manager.link_attributes')->getDefinitions();
    

    Please implement ContainerFactoryPluginInterface for the widget and inject

  5. +++ b/src/Plugin/Field/FieldWidget/LinkWithAttributesWidget.php
    @@ -55,13 +55,15 @@ class LinkWithAttributesWidget extends LinkWidget {
    +        $default_value = isset($attributes[$attribute]) ? $attributes[$attribute] : '';
    +        $element['options']['attributes'][$attribute]['#default_value'] = $default_value;
    

    Nit: let's drop the local variable, not used elsewhere

  6. +++ b/src/Plugin/Field/FieldWidget/LinkWithAttributesWidget.php
    @@ -71,12 +73,16 @@ class LinkWithAttributesWidget extends LinkWidget {
    +    foreach ($plugin_definitions as $plugin_id => $plugin_definition) {
    +      $options[$plugin_id] = $plugin_definition['title'];
    +    }
    

    Can you use array_map here and avoid the foreach, the keys are the same.

Thanks again, great work

seanB’s picture

FileSize
7.42 KB
6.12 KB

Thanks for the review! Addressed all feedback, updated patch is attached.

larowlan’s picture

Status: Needs review » Reviewed & tested by the community
  1. +++ b/src/Plugin/Field/FieldWidget/LinkWithAttributesWidget.php
    @@ -17,7 +21,49 @@ use Drupal\link\Plugin\Field\FieldWidget\LinkWidget;
    +  private $linkAttributesManager;
    

    Nit, private => protected, we don't really use private much in Drupal - can be fixed on commit

  2. +++ b/src/Plugin/Field/FieldWidget/LinkWithAttributesWidget.php
    @@ -55,14 +101,13 @@ class LinkWithAttributesWidget extends LinkWidget {
    +        $element['options']['attributes'][$attribute]['#default_value'] = isset($attributes[$attribute]) ? $attributes[$attribute] : '';
           }
    

    oh for php7 and null coalesce :)

Thanks again

larowlan’s picture

seanB’s picture

Thanks for pointing out those last things! :)

  • larowlan committed f5db542 on 8.x-1.x authored by seanB
    Issue #2893078 by seanB, larowlan: Target option as select field
    
larowlan’s picture

Status: Reviewed & tested by the community » Fixed

Fixed, thanks

Rolling a 1.1 release and updating docs

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.