EntityReferenceDisplayItem.php in Entity Reference Display 8
File
src/Plugin/Field/FieldType/EntityReferenceDisplayItem.php
View source
<?php
namespace Drupal\entity_reference_display\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\OptionsProviderInterface;
class EntityReferenceDisplayItem extends FieldItemBase implements OptionsProviderInterface {
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
return [
'value' => DataDefinition::create('string')
->setLabel(new TranslatableMarkup('Display mode'))
->setRequired(TRUE),
];
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'value' => [
'type' => 'varchar',
'length' => 255,
],
],
];
}
public static function defaultFieldSettings() {
return [
'exclude' => [],
'negate' => FALSE,
] + parent::defaultFieldSettings();
}
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$element = [];
$element['exclude'] = [
'#type' => 'select',
'#title' => $this
->t('Excluded display modes'),
'#description' => $this
->t('Select all display modes which will not be offered.'),
'#options' => $this
->getAllDisplayModes(),
'#default_value' => $this
->getSetting('exclude'),
'#multiple' => TRUE,
];
$element['negate'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Include selected display modes instead of excluding'),
'#description' => $this
->t('If checked, only display modes selected above will be offered.'),
'#default_value' => $this
->getSetting('negate'),
];
return $element;
}
public function isEmpty() {
$value = $this
->get('value')
->getValue();
return $value === NULL || $value === '';
}
public function getPossibleValues(AccountInterface $account = NULL) {
return array_keys($this
->getPossibleOptions($account));
}
public function getPossibleOptions(AccountInterface $account = NULL) {
$display_modes = $this
->getAllDisplayModes();
$exclude = $this
->getSetting('exclude');
$negate = !empty($this
->getSetting('negate'));
$options = [];
foreach ($display_modes as $key => $display_mode) {
if (!$negate && !isset($exclude[$key]) || $negate && isset($exclude[$key])) {
$options[$key] = $display_mode;
}
}
return $options;
}
public function getSettableValues(AccountInterface $account = NULL) {
return array_keys($this
->getSettableOptions($account));
}
public function getSettableOptions(AccountInterface $account = NULL) {
return $this
->getPossibleOptions($account);
}
private function getAllDisplayModes() {
$display_modes = \Drupal::service('entity_display.repository')
->getAllViewModes();
$result = [];
foreach ($display_modes as $modes) {
foreach ($modes as $mode => $info) {
if (!isset($result[$mode])) {
$result[$mode] = $info['label'];
}
}
}
asort($result);
return [
'default' => 'Default',
] + $result;
}
}