-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1449 from danskernesdigitalebibliotek/DDFFORM-810…
…-links-open-in-new-window-tab-v2 Open links in new window - DDFFORM-810
- Loading branch information
Showing
19 changed files
with
218 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Overview | ||
|
||
The DPL Linkit Options module extends the functionality of the Linkit module by | ||
adding an option to open links in a new window or tab. This custom field widget | ||
allows users to specify whether a link should open in the current window or a | ||
new one when creating or editing link fields in Drupal. | ||
|
||
## Configuration | ||
|
||
Once the module is enabled, you can configure the link fields in your content | ||
types to use the new widget: | ||
|
||
1. Navigate to the "Manage form display" page of the content type you want | ||
to configure. | ||
2. Select "DPL Linkit with options" as the widget type. | ||
3. Save the field settings. | ||
|
||
## Usage | ||
|
||
When editing a content item with the configured link field, you will see a new | ||
checkbox option labeled "Open link in new window/tab." Check this box if you | ||
want the link to open in a new window or tab. If left unchecked, the link will | ||
open in the current window. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
name: "DPL Link" | ||
type: module | ||
description: "Add functionality to link field" | ||
package: DPL | ||
core_version_requirement: ^10 || ^11 | ||
dependencies: | ||
- drupal:linkit |
81 changes: 81 additions & 0 deletions
81
web/modules/custom/dpl_link/src/Plugin/Field/FieldWidget/LinkitOptionsWidget.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace Drupal\dpl_link\Plugin\Field\FieldWidget; | ||
|
||
use Drupal\Core\Field\FieldItemListInterface; | ||
use Drupal\Core\Form\FormStateInterface; | ||
use Drupal\link\Plugin\Field\FieldType\LinkItem; | ||
use Drupal\linkit\Plugin\Field\FieldWidget\LinkitWidget; | ||
|
||
/** | ||
* Plugin implementation of the 'dpl_link_options' widget. | ||
* | ||
* @FieldWidget( | ||
* id = "dpl_link_options", | ||
* label = @Translation("DPL Linkit with options"), | ||
* field_types = { | ||
* "link" | ||
* } | ||
* ) | ||
*/ | ||
class LinkitOptionsWidget extends LinkitWidget { | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @return array<mixed> | ||
* See LinkitWidget. | ||
*/ | ||
public static function defaultSettings(): array { | ||
return [ | ||
'target_blank' => 0, | ||
] + parent::defaultSettings(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state): array { | ||
$element = parent::formElement($items, $delta, $element, $form, $form_state); | ||
$item = $items[$delta] ?? NULL; | ||
$target = 'self'; | ||
|
||
if ($item instanceof LinkItem) { | ||
$target = $item->getValue()['options']['target'] ?? $target; | ||
} | ||
|
||
$element['target_blank'] = [ | ||
'#type' => 'checkbox', | ||
'#title' => $this->t('Open link in new window/tab'), | ||
'#default_value' => ($target === '_blank'), | ||
]; | ||
|
||
return $element; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @param array<mixed> $values | ||
* See LinkitWidget. | ||
* @param array<mixed> $form | ||
* See LinkitWidget. | ||
* @param \Drupal\Core\Form\FormStateInterface $form_state | ||
* See LinkitWidget. | ||
* | ||
* @return array<mixed> | ||
* See LinkitWidget. | ||
*/ | ||
public function massageFormValues(array $values, array $form, FormStateInterface $form_state): array { | ||
$values = parent::massageFormValues($values, $form, $form_state); | ||
|
||
foreach ($values as $delta => $value) { | ||
if (!empty($value['target_blank'])) { | ||
$values[$delta]['options']['target'] = '_blank'; | ||
} | ||
} | ||
|
||
return $values; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
web/themes/custom/novel/templates/components/card-grid-link.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
{% for item in items %} | ||
<a href="{{ item.content['#url'] }}" class="card-grid__link">{{ item.content['#title'] }}</a> | ||
{% set url = item.content['#url'] %} | ||
{% set target = url.options.target|default('_self') %} | ||
<a href="{{ url }}" target="{{ target }}" class="card-grid__link link-tag">{{ item.content['#title'] }}</a> | ||
{% endfor %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.