Skip to content
Snippets Groups Projects
Commit 9e9b8a57 authored by Taras Kravchuk's avatar Taras Kravchuk
Browse files

Issue #3483142 by _tarik_: Provide a new formatter to allow output only...

Issue #3483142 by _tarik_: Provide a new formatter to allow output only specific levels of taxonomy terms
parent c56e63aa
No related branches found
No related tags found
1 merge request!27Issue #3483142 by _tarik_: Provide a new formatter to allow output only...
<?php
namespace Drupal\cshs\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Defines the field formatter.
*
* @FieldFormatter(
* id = "cshs_specific_level_taxonomy",
* label = @Translation("Specific level taxonomy"),
* description = @Translation("Shows only specific level of taxonomy terms"),
* field_types = {
* "entity_reference",
* },
* )
*/
class CshsSpecificTaxonomyLevelFormatter extends CshsFormatterBase {
/**
* {@inheritdoc}
*/
public static function defaultSettings(): array {
$settings = parent::defaultSettings();
$settings['level'] = 1;
return $settings;
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state): array {
$element = parent::settingsForm($form, $form_state);
unset($element['reverse']);
$element['level'] = [
'#type' => 'number',
'#min' => 1,
'#title' => $this->t('level'),
'#description' => $this->t('The taxonomy level to show. If the term in the field has a different level it will be hidden. The root level is 1.'),
'#default_value' => $this->getSetting('level'),
];
return $element;
}
/**
* {@inheritdoc}
*/
public function settingsSummary(): array {
$summary = [];
$summary[] = $this->t('Linked to term page: @linked', [
'@linked' => $this->getSetting('linked') ? $this->t('Yes') : $this->t('No'),
]);
$summary[] = $this->t('Level to show: @level', [
'@level' => $this->getSetting('level') ?? $this->t('Root'),
]);
return $summary;
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode): array {
$level = (int) $this->getSetting('level');
$linked = (bool) $this->getSetting('linked');
$elements = [];
foreach ($this->getEntitiesToView($items, $langcode) as $term) {
$tree = $this->getTermParents($term);
if (count($tree) !== $level) {
continue;
}
$term_id = $term->id();
if (!isset($elements[$term_id])) {
$elements[$term_id] = [
'#theme' => 'cshs_term_group',
'#id' => $term_id,
'#title' => $this->getTermsLabels([$term], $linked)[0] ?? $term->label(),
'#terms' => [],
];
}
}
return $elements;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment