You are here

type_style.module in Type Style 8

Hook implementations for the Type Style module.

File

type_style.module
View source
<?php

/**
 * @file
 * Hook implementations for the Type Style module.
 */
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Config\Entity\ThirdPartySettingsInterface;

/**
 * Implements hook_form_alter().
 */
function type_style_form_alter(array &$form, FormStateInterface $form_state) {
  $form_object = $form_state
    ->getFormObject();
  if (!$form_object instanceof EntityForm) {
    return;
  }
  $entity = $form_object
    ->getEntity();
  $entity_support = \Drupal::moduleHandler()
    ->invokeAll('type_style_entity_support');
  if (!$entity instanceof ConfigEntityBundleBase && !in_array($entity
    ->getEntityTypeId(), $entity_support)) {
    return;
  }
  $label = $entity
    ->getEntityType()
    ->getLabel();
  $settings = $entity
    ->getThirdPartySettings('type_style');
  type_style_attach_to_form($form, $settings, $label);
  $form['#entity_builders'][] = 'type_style_entity_builder';
  \Drupal::moduleHandler()
    ->alter('type_style_form', $form, $entity);
}

/**
 * Attaches Type Style elements to a form.
 *
 * @param array &$form
 *   The form array.
 * @param array $settings
 *   An array representing current type_style settings.
 * @param string $label
 *   The label for the styled object.
 */
function type_style_attach_to_form(&$form, $settings, $label) {
  $form['type_style'] = [
    '#type' => 'details',
    '#title' => t('Style settings'),
    '#tree' => TRUE,
  ];
  if (isset($form['additional_settings']) && $form['additional_settings']['#type'] === 'vertical_tabs') {
    $form['type_style']['#group'] = 'additional_settings';
  }
  $form['type_style']['color'] = [
    '#type' => 'color',
    '#title' => t('Color'),
    '#description' => t('A color to associate with this @label.', [
      '@label' => $label,
    ]),
    '#default_value' => isset($settings['color']) ? $settings['color'] : '',
  ];
  $form['type_style']['icon'] = [
    '#type' => 'textfield',
    '#title' => t('Icon name'),
    '#description' => t('An icon to associate with this @label.', [
      '@label' => $label,
    ]),
    '#default_value' => isset($settings['icon']) ? $settings['icon'] : '',
  ];
  $form['#validate'][] = 'type_style_form_validate';
}

/**
 * Validate handler for forms with menu options.
 *
 * @see type_style_form_alter()
 */
function type_style_form_validate(&$form, FormStateInterface $form_state) {
  $color = $form_state
    ->getValue([
    'type_style',
    'color',
  ]);
  $color_regex = '/^#[0-9a-f]{6}$/i';
  if (!empty($color) && !preg_match($color_regex, $color)) {
    $form_state
      ->setErrorByName('type_style', t('The style color must be represented in hexadecimal, i.e. #000000.'));
  }
}

/**
 * Entity builder for the Type Style form.
 *
 * @see type_style_form_alter()
 */
function type_style_entity_builder($entity_type, ThirdPartySettingsInterface $type, &$form, FormStateInterface $form_state) {
  $style_values = $form_state
    ->getValue('type_style');
  foreach ($style_values as $key => $value) {
    $type
      ->setThirdPartySetting('type_style', $key, $value);
  }
}

/**
 * Implements hook_page_attachments().
 */
function type_style_page_attachments(array &$attachments) {
  $config = \Drupal::config('type_style.settings');
  if ($config
    ->get('use_cdn')) {
    $attachments['#attached']['library'][] = 'type_style/' . $config
      ->get('icon_type');
  }
}

/**
 * Helper function to grab a style for a given entity.
 *
 * You can pass a bundle (ex: NodeType) or a bundlable entity (ex: Node).
 *
 * @param \Drupal\Core\Entity\EntityInterface $entity
 *   A bundlable entity, or a bundle entity.
 * @param string $name
 *   The name of the style you want to get (i.e. color, icon).
 * @param mixed $default
 *   A default value in case the style is not set. Defaults to FALSE.
 *
 * @return mixed
 *   The style if set, or the default. This value is safe to use.
 */
function type_style_get_style(EntityInterface $entity, $name, $default = FALSE) {
  $styles = type_style_get_styles($entity);
  return isset($styles[$name]) ? $styles[$name] : $default;
}

/**
 * Helper function to grab all styles for a given entity.
 *
 * You can pass a bundle (ex: NodeType) or a bundlable entity (ex: Node).
 *
 * @param \Drupal\Core\Entity\EntityInterface $entity
 *   A bundlable entity, or a bundle entity.
 *
 * @return array
 *   An associative array of styles. Values are safe to use.
 */
function type_style_get_styles(EntityInterface $entity) {
  $bundle_type = $entity
    ->getEntityType()
    ->getBundleEntityType();
  if (!$entity instanceof ConfigEntityBundleBase && $bundle_type) {
    $entity = \Drupal::entityTypeManager()
      ->getStorage($bundle_type)
      ->load($entity
      ->bundle());
  }
  if ($entity instanceof ThirdPartySettingsInterface) {
    $styles = $entity
      ->getThirdPartySettings('type_style');
  }
  else {
    $styles = [];
  }
  return preg_replace('/[^a-zA-Z0-9\\-\\_\\#]/', '', $styles);
}

Functions

Namesort descending Description
type_style_attach_to_form Attaches Type Style elements to a form.
type_style_entity_builder Entity builder for the Type Style form.
type_style_form_alter Implements hook_form_alter().
type_style_form_validate Validate handler for forms with menu options.
type_style_get_style Helper function to grab a style for a given entity.
type_style_get_styles Helper function to grab all styles for a given entity.
type_style_page_attachments Implements hook_page_attachments().