View source
<?php
namespace Drupal\opigno_certificate\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
class ViewModeSelectorItem extends FieldItemBase {
public static function defaultFieldSettings() {
return [
'view_modes' => [],
];
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('string')
->setLabel(t('View mode'))
->addConstraint('Length', [
'max' => 255,
])
->setRequired(TRUE);
return $properties;
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'value' => [
'type' => 'varchar',
'length' => 255,
],
],
'indexes' => [
'value' => [
'value',
],
],
];
}
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
return [];
}
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$element = [];
$settings = $this
->getSettings();
$entity_type = $this->definition
->getFieldDefinition()
->getTargetEntityTypeId();
$bundle = $this->definition
->getFieldDefinition()
->getTargetBundle();
$view_modes = \Drupal::service('entity_display.repository')
->getViewModeOptionsByBundle($entity_type, $bundle);
foreach ($view_modes as $view_mode_id => $view_mode_label) {
if (!isset($element['view_modes'])) {
$element['view_modes'] = [
'#type' => 'fieldset',
'#tree' => TRUE,
'#title' => t('Available view modes'),
'#attributes' => [
'class' => [
'view-mode-selector-view-modes',
],
],
];
}
$element['view_modes'][$view_mode_id]['enable'] = [
'#type' => 'checkbox',
'#title' => $view_mode_label . ' (' . $view_mode_id . ')',
'#default_value' => isset($settings['view_modes'][$view_mode_id]) && $settings['view_modes'][$view_mode_id]['enable'] ?: FALSE,
];
$element['view_modes'][$view_mode_id]['prefix']['#markup'] = '<div class="settings">';
$element['view_modes'][$view_mode_id]['hide_title'] = [
'#type' => 'checkbox',
'#title' => t('Hide title'),
'#default_value' => isset($settings['view_modes'][$view_mode_id]) && $settings['view_modes'][$view_mode_id]['hide_title'] ?: FALSE,
'#states' => [
'visible' => [
'input[name="field[settings][view_modes][' . $view_mode_id . '][enable]"]' => [
'checked' => TRUE,
],
],
],
];
$element['view_modes'][$view_mode_id]['icon'] = [
'#type' => 'managed_file',
'#title' => t('Icon'),
'#description' => t('An icon which can be used for a view mode preview.'),
'#upload_location' => 'public://view-mode-selector/' . $entity_type,
'#default_value' => isset($settings['view_modes'][$view_mode_id]) && $settings['view_modes'][$view_mode_id]['icon'] ?: 0,
'#hide' => TRUE,
'#states' => [
'visible' => [
'input[name="field[settings][view_modes][' . $view_mode_id . '][enable]"]' => [
'checked' => TRUE,
],
],
],
];
$element['view_modes'][$view_mode_id]['suffix']['#markup'] = '</div>';
}
return $element;
}
}