0

I'm not sure if hook_preprocess_node is the right hook. But I cant find the setting for target in the LinkItem. Can you help me?

A different approach I failed is to extended the FieldWidget of Link and add a checkbox to toggle the target setting. I wasn't able to save this setting though...

class TargetLinkWidget extends LinkWidget {
  public static function defaultSettings() {
    return ['target' => ''] + parent::defaultSettings();
  }

  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $element = parent::formElement($items, $delta, $element, $form, $form_state);

    $element['target'] = [
      '#type' => 'checkbox',
      '#title' => t('Open link in new window'),
      '#value' => '_blank',
      '#default_value' => $this->getSetting('target'),
    ];
    return $element;
  }
}
3
  • Please elaborate: where is the field displayed, in a view as the tags suggest? Then it is fairly easy: load the link twice, one with target set, one without. After that add the checkbox field and rewrite results to be [field_link] and no results behaviour (treat 0 as empty) to [field_link_1]. – Paul Sep 8 '16 at 14:07
  • Thank you. Yes I have a view which displays a bunch of nodes. These all have a link, some external some internal. The editor wants to choose wheither to open the link in a new tab or not. Your idea is great as a temporal workaround. If I understood it right the editor has 2 link fields, one with target blank and one without and toggles them with a checkbox. – Daniel Bammer Sep 9 '16 at 7:25
  • Not quite, I'll explain in my answer. – Paul Sep 9 '16 at 9:28
1

According to your answer to my comment on the question, you are using a view. To have my solution work, it needs to display fields, not teasers.

First off, you do not need to add an additional field to the node type/content type.

All you need to do is to add the already displayed link field in views a second time. All fields get a views internal machine name which is often identical to the field's machine name. However, if you add a field twice, you will end up with two machine names, thus you can determine which version of the rendered field to use at some point.

So, you end up having the same field (Link - field_link) showing up twice in your view's field list. The one added first will be accessible through the token [field_link], the second one by using [field_link_1].

In the first one's settings, choose the plain URL formatter and then go to the "Rewrite result" section. Select the option to output the field as link and enter [field_link] as url and _blank for target:

Formatter option

Activate output as link

Target definition

Finally, set the option "Exclude from display". Repeat that with the second link field, but use [field_link_1] as url and do not set the target.

With both field instances set up and excluded from display, add your checkbox field which may have the machine name field_link_is_external.

Now you need to rewrite that field to be [field_link]:

If the checkbox is checked, the link version with target set "_blank" will be shown.

If the checkbox is checked, the link version with target set "_blank" will be shown.

Next, you need to set the "No result behaviour" as follows:

If the checkbox is not checked, it's value is zero, hence we treat 0 as empty and thus the second link field (w/o target) will be shown.

If the checkbox is not checked, it's value is zero, hence we treat 0 as empty and thus the second link field (w/o target) will be shown.

And that's all there is to it. No need for any editor to enter some url twice. They just need to set the link and decide whether or not to check that checkbox.

1
  • This works great! Even though i did it a little different. I rewrote my Textfield to: <a href="{{ field_kachel_link }}"{% if field_target_blank == '1' %} target="_blank"{% endif %}>{{ field_kachel_text }}</a> My target field is set to output 0/1 and the link plain Text. Thank you so much! – Daniel Bammer Sep 14 '16 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.