You are here

micon_content_type.module in Micon 8

Same filename and directory in other branches
  1. 2.x micon_content_type/micon_content_type.module

File

micon_content_type/micon_content_type.module
View source
<?php

/**
 * @file
 * Contains micon_content_type.module.
 */
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeTypeInterface;
use Drupal\Core\Cache\Cache;

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Adds icon options to the node type form.
 *
 * @see NodeTypeForm::form()
 * @see menu_ui_form_node_type_form_submit()
 */
function micon_content_type_form_node_type_form_alter(&$form, FormStateInterface $form_state) {

  /** @var \Drupal\node\NodeTypeInterface $type */
  $type = $form_state
    ->getFormObject()
    ->getEntity();
  $form['icon'] = [
    '#type' => 'micon',
    '#title' => t('Icon'),
    '#default_value' => micon_content_type_icon($type),
    '#weight' => 0,
  ];
  $form['#entity_builders'][] = 'micon_content_type_form_node_type_form_builder';
}

/**
 * Entity builder for the node type form with menu options.
 *
 * @see menu_ui_form_node_type_form_alter()
 */
function micon_content_type_form_node_type_form_builder($entity_type, NodeTypeInterface $type, &$form, FormStateInterface $form_state) {
  $type
    ->setThirdPartySetting('micon_content_type', 'icon', $form_state
    ->getValue('icon'));
  Cache::invalidateTags([
    'micon.discovery',
  ]);
}

/**
 * Helper function for retrieving the icon from a node type.
 *
 * @var \Drupal\node\NodeTypeInterface $type
 *   The content type.
 */
function micon_content_type_icon(NodeTypeInterface $type) {
  return $type
    ->getThirdPartySetting('micon_content_type', 'icon');
}

/**
 * Implements hook_micon_icons_alter().
 */
function micon_content_type_micon_icons_alter(&$icons) {
  $defaults = [
    'text' => '',
    'regex' => '',
    'weight' => 0,
    'provider' => 'micon_content_type',
  ];
  $types = \Drupal::entityTypeManager()
    ->getStorage('node_type')
    ->loadMultiple();
  foreach ($types as $type) {
    if ($icon = micon_content_type_icon($type)) {
      $id = 'content_type.' . $type
        ->id();
      $icons[$id] = [
        'text' => 'content_type.' . strtolower($type
          ->label()),
        'icon' => $icon,
        'id' => $id,
      ] + $defaults;
      $icons[$id . '_bundle'] = [
        'text' => 'content_type.' . $type
          ->id(),
        'icon' => $icon,
        'id' => $id,
      ] + $defaults;
    }
  }
}

/**
 * Implements hook_entity_type_alter().
 */
function micon_content_type_entity_type_alter(array &$entity_types) {

  /* @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
  $entity_types['node_type']
    ->setListBuilderClass('Drupal\\micon_content_type\\MiconContentTypeListBuilder');
}

Functions

Namesort descending Description
micon_content_type_entity_type_alter Implements hook_entity_type_alter().
micon_content_type_form_node_type_form_alter Implements hook_form_FORM_ID_alter().
micon_content_type_form_node_type_form_builder Entity builder for the node type form with menu options.
micon_content_type_icon Helper function for retrieving the icon from a node type.
micon_content_type_micon_icons_alter Implements hook_micon_icons_alter().