Commit 43a44b42 authored by Gaus Surahman's avatar Gaus Surahman Committed by Gaus Surahman
Browse files

Issue #3156614 by gausarts, inst: add icon to field label

parent ed6ce406
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line

Linkicon 8.x-1.0-dev, 2021-12-24
--------------------------------
- Issue #3156614 by gausarts, inst: add icon to field label.

Linkicon 8.x-1.0-dev, 2021-12-19
--------------------------------
- Added Drupal ^10.
+3 −0
Original line number Diff line number Diff line
@@ -50,6 +50,9 @@ field.formatter.settings.linkicon:
    linkicon_icon_class:
      type: string
      label: 'Additional icon classes'
    linkicon_label_class:
      type: string
      label: 'Additional label classes'
    linkicon_load:
      type: boolean
      label: 'Allow linkicon to provide CSS assets'
+13 −0
Original line number Diff line number Diff line
@@ -56,6 +56,19 @@ function linkicon_field_info_alter(&$info) {
  }
}

/**
 * Implements hook_preprocess_field().
 */
function linkicon_preprocess_field(array &$variables) {
  $element = $variables['element'];
  if (!$variables['label_hidden'] && isset($variables['title_attributes'])) {
    $config = isset($element[0], $element[0]['#config']) ? $element[0]['#config'] : [];
    if (!empty($config['label_class'])) {
      $variables['title_attributes']['class'][] = trim($config['label_class']);
    }
  }
}

/**
 * Implements hook_field_widget_WIDGET_TYPE_form_alter().
 */
+5 −25
Original line number Diff line number Diff line
@@ -3,9 +3,6 @@
namespace Drupal\linkicon\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Path\PathValidatorInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Unicode;
use Drupal\link\Plugin\Field\FieldFormatter\LinkFormatter;
@@ -45,27 +42,10 @@ class LinkIconFormatter extends LinkFormatter {
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $plugin_id,
      $plugin_definition,
      $configuration['field_definition'],
      $configuration['settings'],
      $configuration['label'],
      $configuration['view_mode'],
      $configuration['third_party_settings'],
      $container->get('path.validator'),
      $container->get('renderer'),
      $container->get('linkicon.manager')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, PathValidatorInterface $path_validator, RendererInterface $renderer, LinkIconManagerInterface $linkicon_manager) {
    parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $path_validator);
    $this->renderer        = $renderer;
    $this->linkIconManager = $linkicon_manager;
    $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
    $instance->renderer = $container->get('renderer');
    $instance->linkIconManager = $container->get('linkicon.manager');
    return $instance;
  }

  /**
@@ -209,7 +189,7 @@ class LinkIconFormatter extends LinkFormatter {
      $element = array_merge($info, $element);
    }

    return $element;
    return empty($config['label_class']) ? $element : [$element];
  }

}
+7 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ trait LinkIconFormatterTrait {
      'target'                 => '',
      'linkicon_prefix'        => 'icon',
      'linkicon_icon_class'    => '',
      'linkicon_label_class'   => '',
      'linkicon_wrapper_class' => '',
      'linkicon_load'          => FALSE,
      'linkicon_vertical'      => FALSE,
@@ -72,6 +73,12 @@ trait LinkIconFormatterTrait {
      '#description' => $this->t('Additional icon class for the actual icon apart from <strong>linkicon__icon icon</strong> separated by spaces, e.g: fab fas far.'),
    ];

    $elements['linkicon_label_class'] = [
      '#type'        => 'textfield',
      '#title'       => $this->t('Label classes'),
      '#description' => $this->t('Additional icon class for the field label apart from Drupal <strong>field__label</strong> separated by spaces, e.g: <code>fa far fa-BLAH or icon icon-BLAH</code> depending on vendors. If using Layout Builder, be sure to disable <b>Display title</b> to avoid duplicated labels.'),
    ];

    $elements['linkicon_link'] = [
      '#type'        => 'checkbox',
      '#title'       => $this->t('Add the classes to the A tag.'),