You are here

micon_paragraphs.module in Micon 8

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

File

micon_paragraphs/micon_paragraphs.module
View source
<?php

/**
 * @file
 * Contains micon_paragraphs.module.
 */
use Drupal\Core\Form\FormStateInterface;
use Drupal\paragraphs\ParagraphsTypeInterface;
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_paragraphs_form_paragraphs_type_form_alter(&$form, FormStateInterface $form_state) {

  /** @var \Drupal\paragraphs\ParagraphsTypeInterface $type */
  $type = $form_state
    ->getFormObject()
    ->getEntity();
  $form['icon'] = [
    '#type' => 'micon',
    '#title' => t('Icon'),
    '#default_value' => micon_paragraphs_icon($type),
    '#weight' => 0,
  ];

  // Hide file upload.
  $form['icon_file']['#access'] = FALSE;
  $form['#entity_builders'][] = 'micon_paragraphs_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_paragraphs_form_node_type_form_builder($entity_type, ParagraphsTypeInterface $type, &$form, FormStateInterface $form_state) {
  $type
    ->setThirdPartySetting('micon_paragraphs', 'icon', $form_state
    ->getValue('icon'));
  Cache::invalidateTags([
    'micon.discovery',
  ]);
}

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

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

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

  /* @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
  $entity_types['paragraphs_type']
    ->setListBuilderClass('Drupal\\micon_paragraphs\\MiconParagraphsTypeListBuilder');
}

/**
 * Implements theme_preprocess_paragraphs_add_dialog().
 */
function micon_paragraphs_preprocess_paragraphs_add_dialog(&$variables) {
  foreach ($variables['buttons'] as &$button) {

    // $button['#value'] = micon($button['#value'])->addMatchPrefix('paragraphs');
  }
}

Functions

Namesort descending Description
micon_paragraphs_entity_type_alter Implements hook_entity_type_alter().
micon_paragraphs_form_node_type_form_builder Entity builder for the node type form with menu options.
micon_paragraphs_form_paragraphs_type_form_alter Implements hook_form_FORM_ID_alter().
micon_paragraphs_icon Helper function for retrieving the icon from a node type.
micon_paragraphs_micon_icons_alter Implements hook_micon_icons_alter().
micon_paragraphs_preprocess_paragraphs_add_dialog Implements theme_preprocess_paragraphs_add_dialog().