You are here

public function TaxonomyColor::colorInputBoxs in Fullcalendar View 5.x

Same name and namespace in other branches
  1. 8.3 src/TaxonomyColor.php \Drupal\fullcalendar_view\TaxonomyColor::colorInputBoxs()
  2. 8 src/TaxonomyColor.php \Drupal\fullcalendar_view\TaxonomyColor::colorInputBoxs()
  3. 8.2 src/TaxonomyColor.php \Drupal\fullcalendar_view\TaxonomyColor::colorInputBoxs()
  4. 6.x src/TaxonomyColor.php \Drupal\fullcalendar_view\TaxonomyColor::colorInputBoxs()

Color input box for taxonomy terms of a vocabulary.

File

src/TaxonomyColor.php, line 26

Class

TaxonomyColor
Class TaxonomyColor.

Namespace

Drupal\fullcalendar_view

Code

public function colorInputBoxs($vid, array $defaultValues, $open = FALSE) {

  // Taxonomy color details.
  $elements = [
    '#type' => 'details',
    '#title' => $this
      ->t('Colors for Taxonomies'),
    '#fieldset' => 'colors',
    '#open' => $open,
    '#prefix' => '<div id="color-taxonomies-div">',
    '#suffix' => '</div>',
    '#states' => [
      // Only show this field when the 'vocabularies' is selected.
      'invisible' => [
        [
          ':input[name="style_options[vocabularies]"]' => [
            'value' => '',
          ],
        ],
      ],
    ],
  ];

  // Term IDs of the vocabulary.
  $terms = $this
    ->getTermIds($vid);
  if (isset($terms[$vid])) {

    // Create a color box for each terms.
    foreach ($terms[$vid] as $taxonomy) {

      // If the term name is a valid hex color, use it for the initial default color.
      $initial_color = preg_match('/^#[a-fA-F0-9]{6}$/', $taxonomy->name->value) ? $taxonomy->name->value : '#3a87ad';
      $color = isset($defaultValues[$taxonomy
        ->id()]) ? $defaultValues[$taxonomy
        ->id()] : $initial_color;
      $elements[$taxonomy
        ->id()] = [
        '#title' => $taxonomy->name->value,
        '#default_value' => $color,
        '#type' => 'color',
        '#states' => [
          // Only show this field when the 'tax_field' is selected.
          'invisible' => [
            [
              ':input[name="style_options[tax_field]"]' => [
                'value' => '',
              ],
            ],
          ],
        ],
        '#attributes' => [
          'value' => $color,
          'name' => 'style_options[color_taxonomies][' . $taxonomy
            ->id() . ']',
        ],
      ];
    }
  }
  return $elements;
}