StatusPropertyFormatter.php in Apigee Edge 8
File
src/Plugin/Field/FieldFormatter/StatusPropertyFormatter.php
View source
<?php
namespace Drupal\apigee_edge\Plugin\Field\FieldFormatter;
use Drupal\apigee_edge\Element\StatusPropertyElement;
use Drupal\apigee_edge\Entity\EdgeEntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
class StatusPropertyFormatter extends FormatterBase {
public static function defaultSettings() {
return [
StatusPropertyElement::INDICATOR_STATUS_OK => '',
StatusPropertyElement::INDICATOR_STATUS_WARNING => '',
StatusPropertyElement::INDICATOR_STATUS_ERROR => '',
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
foreach ($this
->getStatusLabels() as $status => $label) {
$form[$status] = [
'#type' => 'textfield',
'#title' => $this
->t('@status_label indicator value', [
'@status_label' => $label,
]),
'#default_value' => $this
->getSetting($status),
];
}
$form['info'] = [
'#markup' => $this
->t('Use lowercase characters only in status values only.'),
];
return $form;
}
public function settingsSummary() {
$summary = parent::settingsSummary();
foreach ($this
->getStatusLabels() as $status => $label) {
$value = $this
->getSetting($status);
$summary[] = $this
->t('@status_label indicator value: @value', [
'@status_label' => $label,
'@value' => $value !== '' ? $value : $this
->t('N/A'),
]);
}
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$element[] = [
'#type' => 'status_property',
'#value' => $items->value,
'#indicator_status' => array_search(mb_strtolower($items->value), $this
->getSettings()) ?: '',
];
return $element;
}
public static function isApplicable(FieldDefinitionInterface $field_definition) {
$entity_type = $field_definition
->getTargetEntityTypeId();
$entity_type_def = \Drupal::entityTypeManager()
->getDefinition($entity_type);
return $field_definition
->getName() === 'status' && in_array(EdgeEntityInterface::class, class_implements($entity_type_def
->getOriginalClass()));
}
private function getStatusLabels() : array {
return [
StatusPropertyElement::INDICATOR_STATUS_OK => $this
->t('OK status'),
StatusPropertyElement::INDICATOR_STATUS_WARNING => $this
->t('Warning status'),
StatusPropertyElement::INDICATOR_STATUS_ERROR => $this
->t('Error status'),
];
}
}