View source
<?php
namespace Drupal\fullcalendar_view;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class TaxonomyColor {
use StringTranslationTrait;
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
$this->entityTypeManager = $entityTypeManager;
}
public function colorInputBoxs($vid, array $defaultValues, $open = FALSE) {
$elements = [
'#type' => 'details',
'#title' => $this
->t('Colors for Taxonomies'),
'#fieldset' => 'colors',
'#open' => $open,
'#prefix' => '<div id="color-taxonomies-div">',
'#suffix' => '</div>',
'#states' => [
'invisible' => [
[
':input[name="style_options[vocabularies]"]' => [
'value' => '',
],
],
],
],
];
$terms = $this
->getTermIds($vid);
if (isset($terms[$vid])) {
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' => [
'invisible' => [
[
':input[name="style_options[tax_field]"]' => [
'value' => '',
],
],
],
],
'#attributes' => [
'value' => $color,
'name' => 'style_options[color_taxonomies][' . $taxonomy
->id() . ']',
],
];
}
}
return $elements;
}
private function getTermIds($vid) {
if (empty($vid)) {
return [];
}
$terms =& drupal_static(__FUNCTION__);
if (!isset($terms[$vid])) {
$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;
}
}