You are here

linkicon.module in Link Icon 8

Same filename and directory in other branches
  1. 7 linkicon.module

A link field formatter to create icon classes based on predefined titles.

File

linkicon.module
View source
<?php

/**
 * @file
 * A link field formatter to create icon classes based on predefined titles.
 */
use Drupal\Core\Form\FormStateInterface;
use Drupal\linkicon\LinkIconManagerInterface;

/**
 * Provides a convenient shortcut for procedural hooks.
 *
 * @return class
 *   The Linkicon manager class instance.
 */
function linkicon() {
  static $manager;
  if (!isset($manager)) {
    $manager = \Drupal::service('linkicon.manager');
  }
  return $manager;
}

/**
 * Implements hook_theme().
 */
function linkicon_theme() {
  return [
    'linkicon' => [
      'render element' => 'element',
      'file' => 'linkicon.theme.inc',
    ],
    'linkicon_item' => [
      'render element' => 'element',
      'file' => 'linkicon.theme.inc',
    ],
  ];
}

/**
 * Implements hook_library_info_build().
 */
function linkicon_library_info_build() {
  return linkicon()
    ->libraryInfoBuild();
}

/**
 * Implements hook_field_info_alter().
 */
function linkicon_field_info_alter(&$info) {
  if (isset($info['link'])) {
    $info['link']['class'] = '\\Drupal\\linkicon\\LinkIconItem';
  }
}

/**
 * Implements hook_field_widget_WIDGET_TYPE_form_alter().
 */
function linkicon_field_widget_link_default_form_alter(&$element, FormStateInterface $form_state, $context) {
  if (isset($element['uri']) && isset($element['title'])) {
    $items = $context['items'];

    // @todo drop 'predefined' for the new integer: LINKICON_PREDEFINED.
    $predefined = $items
      ->getSetting('title') === LinkIconManagerInterface::LINKICON_PREDEFINED || $items
      ->getSetting('title') === 'predefined';
    if ($predefined && ($title_predefined = $items
      ->getSetting('title_predefined')) !== NULL) {
      $options = linkicon()
        ->extractAllowedValues($title_predefined);
      $element['title']['#type'] = 'select';
      $element['title']['#options'] = $options;
      $element['title']['#empty_option'] = t('- Select -');
      $element['title']['#access'] = TRUE;
      $element['#element_validate'] = [
        '\\Drupal\\linkicon\\LinkIconItem::elementValidateLinkIcon',
      ];
    }
  }
}

/**
 * Implements hook_help().
 */
function linkicon_help($route_name) {
  if ($route_name == 'help.page.linkicon') {
    $output = file_get_contents(dirname(__FILE__) . '/README.md');
    return function_exists('blazy_parse_markdown') ? blazy_parse_markdown($output) : '<pre>' . $output . '</pre>';
  }
  return '';
}