Skip to content
Snippets Groups Projects
Commit a671f18b authored by Tess Bakker's avatar Tess Bakker Committed by Jake Bell
Browse files

Issue #3158782 by Tess Bakker, SpadXIII: Linkit widget support

parent 8b554b44
Branches 2.x
No related merge requests found
......@@ -2,11 +2,9 @@
namespace Drupal\micon_link\Plugin\Field\FieldWidget;
use Drupal\Component\Utility\Html;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\link\Plugin\Field\FieldWidget\LinkWidget;
use Drupal\micon\Entity\Micon;
use Drupal\micon\MiconIconizeTrait;
/**
......@@ -23,19 +21,13 @@ use Drupal\micon\MiconIconizeTrait;
class MiconLinkWidget extends LinkWidget {
use MiconIconizeTrait;
use MiconLinkWidgetTrait;
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'placeholder_url' => '',
'placeholder_title' => '',
'target' => FALSE,
'packages' => [],
'icon' => '',
'position' => FALSE,
] + parent::defaultSettings();
return self::prependDefaultSettings(parent::defaultSettings());
}
/**
......@@ -44,35 +36,7 @@ class MiconLinkWidget extends LinkWidget {
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = parent::settingsForm($form, $form_state);
$element['packages'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Icon Packages'),
'#default_value' => $this->getSetting('packages'),
'#description' => t('The icon packages that should be made available in this field. If no packages are selected, all will be made available.'),
'#options' => Micon::loadActiveLabels(),
];
$element['icon'] = [
'#type' => 'micon',
'#title' => $this->t('Default Icon'),
'#default_value' => $this->getSetting('icon'),
];
$element['position'] = [
'#type' => 'checkbox',
'#title' => $this->t('Allow icon position selection'),
'#description' => $this->t('If selected, a "position" select will be made available.'),
'#default_value' => $this->getSetting('position'),
];
$element['target'] = [
'#type' => 'checkbox',
'#title' => $this->t('Allow target selection'),
'#description' => $this->t('If selected, an "open in new window" checkbox will be made available.'),
'#default_value' => $this->getSetting('target'),
];
return $element;
return $this->getSettingsFormElement($element);
}
/**
......@@ -80,102 +44,15 @@ class MiconLinkWidget extends LinkWidget {
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$element['#element_validate'][] = [get_called_class(), 'validateElement'];
$element['title']['#weight'] = -1;
$id = Html::getUniqueId('micon-link-' . $this->fieldDefinition->getName() . '-icon');
$item = $items[$delta];
$options = $item->get('options')->getValue();
$attributes = isset($options['attributes']) ? $options['attributes'] : [];
$element['options']['attributes']['data-icon'] = [
'#type' => 'micon',
'#title' => $this->t('Icon'),
'#id' => $id,
'#default_value' => isset($attributes['data-icon']) ? $attributes['data-icon'] : $this->getSetting('icon'),
'#packages' => $this->getPackages(),
];
if ($this->getSetting('position')) {
$element['options']['attributes']['data-icon-position'] = [
'#type' => 'select',
'#title' => $this->t('Icon position'),
'#options' => [
'before' => $this->t('Before'),
'after' => $this->t('After'),
],
'#default_value' => isset($attributes['data-icon-position']) ? $attributes['data-icon-position'] : NULL,
'#states' => [
'invisible' => [
'#' . $id => ['value' => ''],
],
'optional' => [
'#' . $id => ['value' => ''],
],
],
];
}
if ($this->getSetting('target')) {
$element['options']['attributes']['target'] = [
'#type' => 'checkbox',
'#title' => $this->t('Open link in new window'),
'#description' => $this->t('If selected, the menu link will open in a new window/tab when clicked.'),
'#default_value' => !empty($attributes['target']),
'#return_value' => '_blank',
];
}
return $element;
}
/**
* Get packages available to this field.
*/
protected function getPackages() {
return $this->getSetting('packages');
}
/**
* Recursively clean up options array if no data-icon is set.
*/
public static function validateElement($element, FormStateInterface $form_state, $form) {
$values = $form_state->getValue($element['#parents']);
if (!empty($values)) {
foreach ($values['options']['attributes'] as $attribute => $value) {
if (!empty($value)) {
$values['options']['attributes'][$attribute] = $value;
$values['attributes'][$attribute] = $value;
}
else {
unset($values['options']['attributes'][$attribute]);
unset($values['attributes'][$attribute]);
}
}
}
$form_state->setValueForElement($element, $values);
return $this->getFormElement($items, $delta, $element, $form, $form_state);
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = parent::settingsSummary();
$enabled_packages = array_filter($this->getSetting('packages'));
if ($enabled_packages) {
$enabled_packages = array_intersect_key(Micon::loadActiveLabels(), $enabled_packages);
$summary[] = $this->t('With icon packages: @packages', ['@packages' => implode(', ', $enabled_packages)]);
}
else {
$summary[] = $this->t('With icon packages: @packages', ['@packages' => 'All']);
}
if ($icon = $this->getSetting('icon')) {
$summary[] = $this->micon('Default icon: ')->setIcon($icon)->setIconAfter();
}
if ($this->getSetting('target')) {
$summary[] = $this->t('Allow target selection');
}
return $summary;
return $this->appendSettingsSummary(parent::settingsSummary());
}
}
<?php
namespace Drupal\micon_link\Plugin\Field\FieldWidget;
use Drupal\Component\Utility\Html;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\micon\Entity\Micon;
/**
* Micon Link Widget Trait for Link Widgets.
*
* @package Drupal\micon_link\Plugin\Field\FieldWidget
*/
trait MiconLinkWidgetTrait {
/**
* {@inheritdoc}
*/
public static function prependDefaultSettings(array $defaultSettings = []) {
return [
'placeholder_url' => '',
'placeholder_title' => '',
'target' => FALSE,
'packages' => [],
'icon' => '',
'position' => FALSE,
] + $defaultSettings;
}
/**
* {@inheritdoc}
*/
public function getSettingsFormElement(array $element = []) {
$element['packages'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Icon Packages'),
'#default_value' => $this->getSetting('packages'),
'#description' => t('The icon packages that should be made available in this field. If no packages are selected, all will be made available.'),
'#options' => Micon::loadActiveLabels(),
];
$element['icon'] = [
'#type' => 'micon',
'#title' => $this->t('Default Icon'),
'#default_value' => $this->getSetting('icon'),
];
$element['position'] = [
'#type' => 'checkbox',
'#title' => $this->t('Allow icon position selection'),
'#description' => $this->t('If selected, a "position" select will be made available.'),
'#default_value' => $this->getSetting('position'),
];
$element['target'] = [
'#type' => 'checkbox',
'#title' => $this->t('Allow target selection'),
'#description' => $this->t('If selected, an "open in new window" checkbox will be made available.'),
'#default_value' => $this->getSetting('target'),
];
return $element;
}
/**
* {@inheritdoc}
*/
public function getFormElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element['#element_validate'][] = [get_called_class(), 'validateElement'];
$id = Html::getUniqueId('micon-link-' . $this->fieldDefinition->getName() . '-icon');
$item = $items[$delta];
$options = $item->get('options')->getValue();
$attributes = isset($options['attributes']) ? $options['attributes'] : [];
$element['options']['attributes']['data-icon'] = [
'#type' => 'micon',
'#title' => $this->t('Icon'),
'#id' => $id,
'#default_value' => isset($attributes['data-icon']) ? $attributes['data-icon'] : $this->getSetting('icon'),
'#packages' => $this->getPackages(),
];
$element['options']['attributes']['data-icon-position'] = [
'#type' => 'select',
'#title' => $this->t('Icon position'),
'#options' => [
'before' => $this->t('Before'),
'after' => $this->t('After'),
],
'#default_value' => isset($attributes['data-icon-position']) ? $attributes['data-icon-position'] : NULL,
'#states' => [
'invisible' => [
'#' . $id => ['value' => ''],
],
'optional' => [
'#' . $id => ['value' => ''],
],
],
];
if ($this->getSetting('target')) {
$element['options']['attributes']['target'] = [
'#type' => 'checkbox',
'#title' => t('Open link in new window'),
'#description' => t('If selected, the menu link will open in a new window/tab when clicked.'),
'#default_value' => !empty($attributes['target']),
'#return_value' => '_blank',
];
}
return $element;
}
/**
* Get packages available to this field.
*/
protected function getPackages() {
return $this->getSetting('packages');
}
/**
* Recursively clean up options array if no data-icon is set.
*/
public static function validateElement($element, FormStateInterface $form_state, $form) {
$values = $form_state->getValue($element['#parents']);
if (!empty($values)) {
foreach ($values['options']['attributes'] as $attribute => $value) {
if (!empty($value)) {
$values['options']['attributes'][$attribute] = $value;
$values['attributes'][$attribute] = $value;
}
else {
unset($values['options']['attributes'][$attribute]);
unset($values['attributes'][$attribute]);
}
}
}
$form_state->setValueForElement($element, $values);
}
/**
* {@inheritdoc}
*/
public function appendSettingsSummary(array $summary = []) {
$enabled_packages = array_filter($this->getSetting('packages'));
if ($enabled_packages) {
$enabled_packages = array_intersect_key(Micon::loadActiveLabels(), $enabled_packages);
$summary[] = $this->t('With icon packages: @packages', [
'@packages' => implode(', ', $enabled_packages),
]);
}
else {
$summary[] = $this->t('With icon packages: @packages', ['@packages' => 'All']);
}
if ($icon = $this->getSetting('icon')) {
$summary[] = $this->micon('Default icon: ')
->setIcon($icon)
->setIconAfter();
}
if ($this->getSetting('target')) {
$summary[] = $this->t('Allow target selection');
}
return $summary;
}
}
## Micon Linkit
Provides an alternate linkit widget for the link field that allows setting icons.
name: Micon Linkit
type: module
description: Provides an alternate linkit widget for the link field that allows setting icons.
core_version_requirement: ^8 || ^9
package: Micon
dependencies:
- micon:micon_link
- linkit:linkit
<?php
/**
* @file
* Contains micon_linkit.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function micon_linkit_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the micon_linkit module.
case 'help.page.micon_linkit':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Micon link module provides a linkit widget that allows users to add icons to link fields.') . '</p>';
return $output;
default:
}
}
<?php
namespace Drupal\micon_linkit\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\linkit\Plugin\Field\FieldWidget\LinkitWidget;
use Drupal\micon\MiconIconizeTrait;
use Drupal\micon_link\Plugin\Field\FieldWidget\MiconLinkWidgetTrait;
/**
* Plugin implementation of the 'linkit' widget.
*
* @FieldWidget(
* id = "micon_linkit",
* label = @Translation("Linkit (with icon)"),
* field_types = {
* "link"
* }
* )
*/
class MiconLinkitWidget extends LinkitWidget {
use MiconIconizeTrait;
use MiconLinkWidgetTrait;
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return self::prependDefaultSettings(parent::defaultSettings());
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = parent::settingsForm($form, $form_state);
return $this->getSettingsFormElement($element);
}
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
return $this->getFormElement($items, $delta, $element, $form, $form_state);
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
return $this->appendSettingsSummary(parent::settingsSummary());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment