You are here

function template_preprocess_calendar_stripe_legend in Calendar 8

Format a node stripe legend.

Parameters

array $vars: An array with variables.

Return value

string The HTML output of the legend.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

./calendar.theme.inc, line 727
Theme functions for the Calendar module.

Code

function template_preprocess_calendar_stripe_legend(array &$vars) {
  if (empty($vars) || !($view = $vars['view_and_display_id'])) {
    return '';
  }
  list($view_name, $display_id) = explode(':', $view);
  $view = Views::getView($view_name);
  $view
    ->setDisplay($display_id);
  $row_options = $view->display_handler
    ->getOption('row')['options'];
  $legend_type = $row_options['colors']['legend'];
  $display_options = $row_options['colors']['calendar_colors_' . $legend_type];
  $options = [];
  switch ($legend_type) {
    case 'type':
      $options = node_type_get_names();
      break;

    // @todo handle taxonomy legends
    case 'taxonomy':
      $vocabularies = (array) $row_options['colors']['calendar_colors_vocabulary'];
      $term_colors = $row_options['colors']['calendar_colors_taxonomy'];
      foreach ($vocabularies as $field_name => $vid) {
        $vocab = \Drupal::entityTypeManager()
          ->getStorage("taxonomy_term")
          ->loadTree($vid);
        foreach ($vocab as $key => $term) {
          $options[$term->tid] = $term->name;
        }
      }
      break;
  }
  $headers = [
    [
      'label' => t('Item'),
    ],
    [
      'label' => t('Key'),
    ],
  ];
  $rows = [];
  foreach ($options as $key => $label) {
    $stripe = array_key_exists($key, $display_options) ? $display_options[$key] : CALENDAR_EMPTY_STRIPE;
    if ($stripe != CALENDAR_EMPTY_STRIPE) {
      $rows[] = [
        'label' => $label,
        'stripe' => $stripe,
      ];
    }
  }
  if (!empty($rows)) {
    $vars['headers'] = $headers;
    $vars['rows'] = $rows;
  }
}