You are here

class TaxonomyColor in Fullcalendar View 8.2

Same name and namespace in other branches
  1. 8.3 src/TaxonomyColor.php \Drupal\fullcalendar_view\TaxonomyColor
  2. 8 src/TaxonomyColor.php \Drupal\fullcalendar_view\TaxonomyColor
  3. 6.x src/TaxonomyColor.php \Drupal\fullcalendar_view\TaxonomyColor
  4. 5.x src/TaxonomyColor.php \Drupal\fullcalendar_view\TaxonomyColor

Class TaxonomyColor.

Hierarchy

Expanded class hierarchy of TaxonomyColor

1 file declares its use of TaxonomyColor
FullCalendarDisplay.php in src/Plugin/views/style/FullCalendarDisplay.php
1 string reference to 'TaxonomyColor'
fullcalendar_view.services.yml in ./fullcalendar_view.services.yml
fullcalendar_view.services.yml
1 service uses TaxonomyColor
fullcalendar_view.taxonomy_color in ./fullcalendar_view.services.yml
Drupal\fullcalendar_view\TaxonomyColor

File

src/TaxonomyColor.php, line 11

Namespace

Drupal\fullcalendar_view
View source
class TaxonomyColor {
  use StringTranslationTrait;
  protected $entityTypeManager;

  /**
   * Constructor.
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager) {
    $this->entityTypeManager = $entityTypeManager;
  }

  /**
   * Color input box for taxonomy terms of a vocabulary.
   */
  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) {
        $color = isset($defaultValues[$taxonomy
          ->id()]) ? $defaultValues[$taxonomy
          ->id()] : '#3a87ad';
        $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;
  }

  /**
   * Get all terms of a vocabulary.
   */
  private function getTermIds($vid) {
    if (empty($vid)) {
      return [];
    }
    $terms =& drupal_static(__FUNCTION__);

    // Get taxonomy terms from database if they haven't been loaded.
    if (!isset($terms[$vid])) {

      // Get terms Ids.
      $query = $this->entityTypeManager
        ->getStorage('taxonomy_term')
        ->getQuery();
      $query
        ->condition('vid', $vid);
      $tids = $query
        ->execute();
      $terms[$vid] = $this->entityTypeManager
        ->getStorage('taxonomy_term')
        ->loadMultiple($tids);
    }
    return $terms;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TaxonomyColor::$entityTypeManager protected property
TaxonomyColor::colorInputBoxs public function Color input box for taxonomy terms of a vocabulary.
TaxonomyColor::getTermIds private function Get all terms of a vocabulary.
TaxonomyColor::__construct public function Constructor.