CshsFormatterBase.php in Client-side Hierarchical Select 8.2
File
src/Plugin/Field/FieldFormatter/CshsFormatterBase.php
View source
<?php
namespace Drupal\cshs\Plugin\Field\FieldFormatter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase;
use Drupal\cshs\IsApplicable;
use Drupal\cshs\TaxonomyStorages;
use Drupal\taxonomy\TermInterface;
abstract class CshsFormatterBase extends EntityReferenceFormatterBase {
use IsApplicable;
use TaxonomyStorages;
public static function defaultSettings() : array {
return [
'linked' => FALSE,
'reverse' => FALSE,
];
}
public function settingsForm(array $form, FormStateInterface $form_state) : array {
$element = [];
$element['linked'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Link to term page'),
'#default_value' => $this
->getSetting('linked'),
];
$element['reverse'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Reverse order view'),
'#default_value' => $this
->getSetting('reverse'),
'#description' => $this
->t('Outputs hierarchy in reverse order (the deepest level first).'),
];
return $element;
}
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('Reverse order view: @reverse', [
'@reverse' => $this
->getSetting('reverse') ? $this
->t('Yes') : $this
->t('No'),
]);
return $summary;
}
protected function getTermsLabels(array $list, bool $linked) : array {
$terms = [];
foreach ($list as $item) {
$item = $this
->getTranslationFromContext($item);
$label = $item
->label();
$terms[] = $linked ? $item
->toLink($label)
->toString() : $label;
}
return $terms;
}
protected function getTermParents(TermInterface $term) : array {
return \array_reverse($this
->getTermStorage()
->loadAllParents($term
->id()));
}
}