You are here

function template_preprocess_flags in Flags 8

Prepares variables for flags.

Parameters

$variables:

File

./flags.module, line 22

Code

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']);
}