Skip to content

Instantly share code, notes, and snippets.

@Greg-Boggs
Created July 24, 2024 23:20
Show Gist options
  • Save Greg-Boggs/f13d1cc765c6b741e4b61c39755e6bd4 to your computer and use it in GitHub Desktop.
Save Greg-Boggs/f13d1cc765c6b741e4b61c39755e6bd4 to your computer and use it in GitHub Desktop.
only view selected taxonomy terms
<?php
namespace Drupal\libevents\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceEntityFormatter;
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
/**
* Plugin implementation of the 'boolean_taxonomy_term_formatter' formatter.
*
* @FieldFormatter(
* id = "boolean_taxonomy_term_formatter",
* label = @Translation("Boolean Taxonomy Term Formatter"),
* field_types = {
* "entity_reference"
* }
* )
*/
class BooleanTaxonomyTermFormatter extends EntityReferenceEntityFormatter {
/**
* {@inheritdoc}
*/
protected function getEntitiesToView(EntityReferenceFieldItemListInterface $items, $langcode) {
$entities = parent::getEntitiesToView($items, $langcode);
$entity = $items->getEntity();
$boolean_field_name = $this->getSetting('boolean_field_name');
$taxonomy_term_field_name = $this->getSetting('taxonomy_term_field_name');
$boolean_values = $entity->get($boolean_field_name)->getValue();
$taxonomy_terms = $entity->get($taxonomy_term_field_name)->referencedEntities();
$selected_term = null;
if (!empty($boolean_values[0]['value'])) {
$selected_term = $taxonomy_terms[0] ?? null;
} elseif (!empty($boolean_values[1]['value'])) {
$selected_term = $taxonomy_terms[1] ?? null;
} else {
$selected_term = $taxonomy_terms[2] ?? ($taxonomy_terms[0] ?? null);
}
return $selected_term ? [$selected_term] : [];
}
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'boolean_field_name' => '',
'taxonomy_term_field_name' => '',
'view_mode' => 'default',
] + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = parent::settingsForm($form, $form_state);
$element['boolean_field_name'] = [
'#type' => 'textfield',
'#title' => $this->t('Boolean field name'),
'#default_value' => $this->getSetting('boolean_field_name'),
'#required' => TRUE,
];
$element['taxonomy_term_field_name'] = [
'#type' => 'textfield',
'#title' => $this->t('Taxonomy term field name'),
'#default_value' => $this->getSetting('taxonomy_term_field_name'),
'#required' => TRUE,
];
$element['view_mode'] = [
'#type' => 'textfield',
'#title' => $this->t('View mode'),
'#default_value' => $this->getSetting('view_mode'),
'#required' => TRUE,
];
return $element;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = [];
$summary[] = $this->t('Boolean field: @boolean_field', ['@boolean_field' => $this->getSetting('boolean_field_name')]);
$summary[] = $this->t('Taxonomy term field: @taxonomy_term_field', ['@taxonomy_term_field' => $this->getSetting('taxonomy_term_field_name')]);
$summary[] = $this->t('View mode: @view_mode', ['@view_mode' => $this->getSetting('view_mode')]);
return $summary;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment