DropdownFieldFormatter.php in Open Social 10.2.x
Same filename and directory in other branches
- 8.9 modules/custom/dropdown/src/Plugin/Field/FieldFormatter/DropdownFieldFormatter.php
- 8 modules/custom/dropdown/src/Plugin/Field/FieldFormatter/DropdownFieldFormatter.php
- 8.2 modules/custom/dropdown/src/Plugin/Field/FieldFormatter/DropdownFieldFormatter.php
- 8.3 modules/custom/dropdown/src/Plugin/Field/FieldFormatter/DropdownFieldFormatter.php
- 8.4 modules/custom/dropdown/src/Plugin/Field/FieldFormatter/DropdownFieldFormatter.php
- 8.5 modules/custom/dropdown/src/Plugin/Field/FieldFormatter/DropdownFieldFormatter.php
- 8.6 modules/custom/dropdown/src/Plugin/Field/FieldFormatter/DropdownFieldFormatter.php
- 8.7 modules/custom/dropdown/src/Plugin/Field/FieldFormatter/DropdownFieldFormatter.php
- 8.8 modules/custom/dropdown/src/Plugin/Field/FieldFormatter/DropdownFieldFormatter.php
- 10.3.x modules/custom/dropdown/src/Plugin/Field/FieldFormatter/DropdownFieldFormatter.php
- 10.0.x modules/custom/dropdown/src/Plugin/Field/FieldFormatter/DropdownFieldFormatter.php
- 10.1.x modules/custom/dropdown/src/Plugin/Field/FieldFormatter/DropdownFieldFormatter.php
File
modules/custom/dropdown/src/Plugin/Field/FieldFormatter/DropdownFieldFormatter.phpView source
<?php
namespace Drupal\dropdown\Plugin\Field\FieldFormatter;
use Drupal\Component\Utility\Html;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
/**
* Plugin implementation of the 'dropdown_field_formatter' formatter.
*
* @FieldFormatter(
* id = "dropdown_field_formatter",
* label = @Translation("Dropdown field formatter"),
* field_types = {
* "dropdown"
* }
* )
*/
class DropdownFieldFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
$elements[$delta] = [
'#markup' => $this
->viewValue($item),
];
}
return $elements;
}
/**
* Generate the output appropriate for one field item.
*
* @param \Drupal\Core\Field\FieldItemInterface $item
* One field item.
*
* @return string
* The textual output generated.
*/
protected function viewValue(FieldItemInterface $item) {
// The text value has no text format assigned to it, so the user input
// should equal the output, including newlines.
return $this
->getLabelForValue($item->value);
}
/**
* Display dropdown labels.
*/
public function getLabelForValue($value) {
$settings = $this
->getFieldSettings();
$allowed_values = $settings['allowed_values'];
foreach ($allowed_values as $allowed_value) {
if (isset($allowed_value['value']) && $allowed_value['value'] === $value) {
$label = $allowed_value['label'];
return Html::escape($label);
}
}
}
}
Classes
Name | Description |
---|---|
DropdownFieldFormatter | Plugin implementation of the 'dropdown_field_formatter' formatter. |