You are here

flags.module in Flags 8

File

flags.module
View source
<?php

use Drupal\Core\Template\Attribute;
use Drupal\flags\Mapping\FlagMappingInterface;

/**
 * Implements hook_theme().
 */
function flags_theme($existing, $type, $theme, $path) {
  return array(
    'flags' => array(
      'variables' => array(
        'code' => null,
        'source' => $type,
        'tag' => 'span',
        'attributes' => array(),
      ),
    ),
  );
}

/**
 * Prepares variables for flags.
 *
 * @param $variables
 */
function template_preprocess_flags(&$variables) {
  $flag = $variables['code'];

  // Use mapping service to allow others to provide their own mappings.
  // Try to invoke source specific service if it exists.
  $serviceName = 'flags.mapping.' . $variables['source'];
  if (\Drupal::hasService($serviceName)) {
    $flag = \Drupal::service($serviceName)
      ->map($flag);
    $extra = \Drupal::service($serviceName)
      ->getExtraClasses();
  }
  else {
    throw new \InvalidArgumentException(sprintf('Service %s to map source "%s" is not defined.', $serviceName, $variables['source']));
  }
  $variables['flag'] = $flag;
  if (empty($variables['attributes'])) {
    $variables['attributes'] = new Attribute();
  }
  if (empty($variables['attributes']['class'])) {
    $variables['attributes']['class'] = array();
  }
  $variables['attributes']['class'][] = 'flag';
  $variables['attributes']['class'][] = 'flag-' . $flag;
  if (!empty($extra)) {
    $variables['attributes']
      ->addClass($extra);
  }
  unset($variables['source']);
}

Functions

Namesort descending Description
flags_theme Implements hook_theme().
template_preprocess_flags Prepares variables for flags.