You are here

class EntityReferenceLayoutRevisioned in Entity Reference with Layout 8

Entity Reference Layout Revisioned field type.

Plugin annotation


@FieldType(
  id = "entity_reference_layout_revisioned",
  label = @Translation("Entity Reference with Layout"),
  description = @Translation("An entity field with layouts containing revisioned entity references."),
  category = @Translation("Reference revisions"),
  default_widget = "entity_reference_layout_widget",
  default_formatter = "entity_reference_layout",
  list_class = "\Drupal\entity_reference_layout\EntityReferenceLayoutRevisionsFieldItemList",
)

Hierarchy

Expanded class hierarchy of EntityReferenceLayoutRevisioned

2 files declare their use of EntityReferenceLayoutRevisioned
EntityReferenceLayoutItemNormalizer.php in src/Normalizer/EntityReferenceLayoutItemNormalizer.php
entity_reference_layout.install in ./entity_reference_layout.install
Contains entity_reference_layout.install.

File

src/Plugin/Field/FieldType/EntityReferenceLayoutRevisioned.php, line 25

Namespace

Drupal\entity_reference_layout\Plugin\Field\FieldType
View source
class EntityReferenceLayoutRevisioned extends EntityReferenceRevisionsItem {
  use StringTranslationTrait;

  /**
   * Define field properties.
   */
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
    $properties = parent::propertyDefinitions($field_definition);
    $properties['region'] = DataDefinition::create('string')
      ->setLabel(t('Region'));
    $properties['layout'] = DataDefinition::create('string')
      ->setLabel(t('Layout'));
    $properties['section_id'] = DataDefinition::create('integer')
      ->setLabel(t('Section ID'));
    $properties['options'] = DataDefinition::create('any')
      ->setLabel(t('Options'));
    $properties['config'] = DataDefinition::create('any')
      ->setLabel(t('Config'));
    return $properties;
  }

  /**
   * Define field schema.
   */
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
    $schema = parent::schema($field_definition);
    $schema['columns']['region'] = [
      'type' => 'varchar',
      'length' => '60',
      'not null' => FALSE,
    ];
    $schema['columns']['layout'] = [
      'type' => 'varchar',
      'length' => '60',
      'not null' => FALSE,
    ];
    $schema['columns']['section_id'] = [
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => FALSE,
    ];
    $schema['columns']['options'] = [
      'type' => 'blob',
      'size' => 'normal',
      'serialize' => TRUE,
      'not null' => FALSE,
    ];
    $schema['columns']['config'] = [
      'type' => 'blob',
      'size' => 'normal',
      'serialize' => TRUE,
      'not null' => FALSE,
    ];
    return $schema;
  }

  /**
   * Manipulate field data to be saved as configuration.
   */
  public static function fieldSettingsToConfigData(array $settings) {
    $settings = parent::fieldSettingsToConfigData($settings);
    $allowed_layouts = [];
    $selected_layouts = isset($settings['handler_settings']['allowed_layouts']) ? $settings['handler_settings']['allowed_layouts'] : [];
    $layout_groups = \Drupal::service('plugin.manager.core.layout')
      ->getLayoutOptions();
    foreach ($layout_groups as $group => $layouts) {
      foreach ($layouts as $name => $value) {
        if (!empty($selected_layouts[$group][$name])) {
          $allowed_layouts[$group][$name] = $value;
        }
      }
    }
    $settings['handler_settings']['allowed_layouts'] = $allowed_layouts;
    return $settings;
  }

  /**
   * Manipulate saved data into configuration.
   *
   * Set 'layout_bundles' configuration item as an array even though it
   * may be stored as a string in case we support attaching layouts to
   * multiple bundles in the future.
   */
  public static function fieldSettingsFromConfigData(array $settings) {
    if (isset($settings['handler_settings']['layout_bundles']) && !is_array($settings['handler_settings']['layout_bundles'])) {
      $settings['handler_settings']['layout_bundles'] = [
        $settings['handler_settings']['layout_bundles'],
      ];
    }
    return $settings;
  }

  /**
   * Manipulate configuration data for settings form.
   */
  public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
    $form = parent::fieldSettingsForm($form, $form_state);
    $handler_settings = $this
      ->getSetting('handler_settings');
    $target_type = $this
      ->getSetting('target_type');
    $negate = isset($handler_settings['negate']) ? $handler_settings['negate'] : 0;
    $input_negate = $form_state
      ->getValue([
      'settings',
      'handler_settings',
      'negate',
    ]);
    if (isset($input_negate)) {
      $negate = $input_negate;
    }
    $form['handler']['handler_settings']['negate']['#ajax'] = TRUE;
    $target_bundles = isset($handler_settings['target_bundles']) ? $handler_settings['target_bundles'] : [];

    // For AJAX, also look at form_state values:
    $input_target_bundles = $form_state
      ->getValue([
      'settings',
      'handler_settings',
      'target_bundles',
    ]);
    if (!empty($input_target_bundles)) {
      $target_bundles = $input_target_bundles;
    }
    $target_bundle_options = $form['handler']['handler_settings']['target_bundles']['#options'];
    if ($negate) {
      $layout_bundle_options = array_diff_key($target_bundle_options, $target_bundles);
    }
    else {
      $layout_bundle_options = array_intersect_key($target_bundle_options, $target_bundles);
    }
    if (!empty($form['handler']['handler_settings']['target_bundles_drag_drop'])) {
      foreach (Element::children($form['handler']['handler_settings']['target_bundles_drag_drop']) as $item) {
        $form['handler']['handler_settings']['target_bundles_drag_drop'][$item]['enabled']['#ajax'] = TRUE;
      }
    }
    $default_layout_bundle = '';
    if (isset($handler_settings['layout_bundles'])) {
      $default_layout_bundle = reset($handler_settings['layout_bundles']);
    }
    if (empty($layout_bundle_options[$default_layout_bundle])) {
      $default_layout_bundle = [];
    }
    $form['handler']['handler_settings']['layout_bundles'] = [
      '#type' => 'radios',
      '#options' => $layout_bundle_options,
      '#title' => $this
        ->t('Layout @target_type type', [
        '@target_type' => $target_type,
      ]),
      '#default_value' => $default_layout_bundle,
      '#multiple' => TRUE,
      '#description' => $this
        ->t('Which @target_type type should be used for layout.', [
        '@target_type' => $target_type,
      ]),
      '#required' => TRUE,
      '#id' => 'erl-layout-bundles-select',
    ];
    $layout_groups = \Drupal::service('plugin.manager.core.layout')
      ->getLayoutOptions();
    $layout_groups_defaults = isset($handler_settings['allowed_layouts']) ? $handler_settings['allowed_layouts'] : [];
    foreach ($layout_groups as $group => $layouts) {
      if (!empty($group)) {
        $defaults = isset($layout_groups_defaults[$group]) ? array_keys($layout_groups_defaults[$group]) : [];
        $form['handler']['handler_settings']['allowed_layouts'][$group] = [
          '#type' => 'checkboxes',
          '#options' => $layouts,
          '#title' => $group,
          '#multiple' => TRUE,
          '#default_value' => $defaults,
        ];
      }
    }
    $form['handler']['handler_settings']['allowed_layouts']['#prefix'] = '<b>' . $this
      ->t('Allowed Layouts:') . '</b>';
    return $form;
  }

  /**
   * Get defaults settings.
   */
  public static function defaultFieldSettings() {
    $settings = parent::defaultFieldSettings();
    $settings['handler_settings']['layout_bundles'] = [];
    $settings['handler_settings']['allowed_layouts'] = [];
    return $settings;
  }

  /**
   * {@inheritdoc}
   *
   * Only support references to paragraphs.
   *
   * @todo Expand support to other entity types.
   */
  public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
    $element = parent::storageSettingsForm($form, $form_state, $has_data);
    foreach ($element['target_type']['#options'] as $key => $value) {
      if ($key !== 'paragraph') {
        unset($element['target_type']['#options'][$key]);
      }
    }
    return $element;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EntityReferenceItem::calculateDependencies public static function Calculates dependencies for field items. Overrides FieldItemBase::calculateDependencies
EntityReferenceItem::calculateStorageDependencies public static function Calculates dependencies for field items on the storage level. Overrides FieldItemBase::calculateStorageDependencies
EntityReferenceItem::defaultStorageSettings public static function Defines the storage-level settings for this plugin. Overrides FieldItemBase::defaultStorageSettings 1
EntityReferenceItem::fieldSettingsAjaxProcess public static function Render API callback: Processes the field settings form and allows access to the form state.
EntityReferenceItem::fieldSettingsAjaxProcessElement public static function Adds entity_reference specific properties to AJAX form elements from the field settings form.
EntityReferenceItem::fieldSettingsFormValidate public static function Form element validation handler; Invokes selection plugin's validation.
EntityReferenceItem::formProcessMergeParent public static function Render API callback: Moves entity_reference specific Form API elements (i.e. 'handler_settings') up a level for easier processing by the validation and submission handlers.
EntityReferenceItem::getConstraints public function Gets a list of validation constraints. Overrides TypedData::getConstraints
EntityReferenceItem::getPossibleOptions public function Returns an array of possible values with labels for display. Overrides OptionsProviderInterface::getPossibleOptions
EntityReferenceItem::getPossibleValues public function Returns an array of possible values. Overrides OptionsProviderInterface::getPossibleValues
EntityReferenceItem::getRandomBundle protected static function Gets a bundle for a given entity type and selection options.
EntityReferenceItem::getSettableOptions public function Returns an array of settable values with labels for display. Overrides OptionsProviderInterface::getSettableOptions
EntityReferenceItem::getSettableValues public function Returns an array of settable values. Overrides OptionsProviderInterface::getSettableValues
EntityReferenceItem::hasNewEntity public function Determines whether the item holds an unsaved entity.
EntityReferenceItem::mainPropertyName public static function Returns the name of the main property, if any. Overrides FieldItemBase::mainPropertyName
EntityReferenceItem::settingsAjax public static function Ajax callback for the handler settings form.
EntityReferenceItem::settingsAjaxSubmit public static function Submit handler for the non-JS case.
EntityReferenceLayoutRevisioned::defaultFieldSettings public static function Get defaults settings. Overrides EntityReferenceItem::defaultFieldSettings
EntityReferenceLayoutRevisioned::fieldSettingsForm public function Manipulate configuration data for settings form. Overrides EntityReferenceItem::fieldSettingsForm
EntityReferenceLayoutRevisioned::fieldSettingsFromConfigData public static function Manipulate saved data into configuration. Overrides FieldItemBase::fieldSettingsFromConfigData
EntityReferenceLayoutRevisioned::fieldSettingsToConfigData public static function Manipulate field data to be saved as configuration. Overrides FieldItemBase::fieldSettingsToConfigData
EntityReferenceLayoutRevisioned::propertyDefinitions public static function Define field properties. Overrides EntityReferenceRevisionsItem::propertyDefinitions
EntityReferenceLayoutRevisioned::schema public static function Define field schema. Overrides EntityReferenceRevisionsItem::schema
EntityReferenceLayoutRevisioned::storageSettingsForm public function Only support references to paragraphs. Overrides EntityReferenceRevisionsItem::storageSettingsForm
EntityReferenceRevisionsItem::delete public function Defines custom delete behavior for field values. Overrides FieldItemBase::delete
EntityReferenceRevisionsItem::deleteRevision public function Defines custom revision delete behavior for field values. Overrides FieldItemBase::deleteRevision
EntityReferenceRevisionsItem::generateSampleValue public static function Generates placeholder field values. Overrides EntityReferenceItem::generateSampleValue
EntityReferenceRevisionsItem::getPreconfiguredOptions public static function Returns preconfigured field options for a field type. Overrides EntityReferenceItem::getPreconfiguredOptions
EntityReferenceRevisionsItem::getValue public function Gets the data value. Overrides EntityReferenceItem::getValue
EntityReferenceRevisionsItem::isEmpty public function Determines whether the data structure is empty. Overrides EntityReferenceItem::isEmpty
EntityReferenceRevisionsItem::onChange public function React to changes to a child property or item. Overrides EntityReferenceItem::onChange
EntityReferenceRevisionsItem::onDependencyRemoval public static function Informs the plugin that a dependency of the field will be deleted. Overrides EntityReferenceItem::onDependencyRemoval
EntityReferenceRevisionsItem::postSave public function Defines custom post-save behavior for field values. Overrides FieldItemBase::postSave
EntityReferenceRevisionsItem::preSave public function Defines custom presave behavior for field values. Overrides EntityReferenceItem::preSave
EntityReferenceRevisionsItem::setValue public function Sets the data value. Overrides EntityReferenceItem::setValue
FieldItemBase::getEntity public function Gets the entity that field belongs to. Overrides FieldItemInterface::getEntity
FieldItemBase::getFieldDefinition public function Gets the field definition. Overrides FieldItemInterface::getFieldDefinition
FieldItemBase::getLangcode public function Gets the langcode of the field values held in the object. Overrides FieldItemInterface::getLangcode
FieldItemBase::getSetting protected function Returns the value of a field setting.
FieldItemBase::getSettings protected function Returns the array of field settings.
FieldItemBase::storageSettingsFromConfigData public static function Returns a settings array in the field type's canonical representation. Overrides FieldItemInterface::storageSettingsFromConfigData 2
FieldItemBase::storageSettingsToConfigData public static function Returns a settings array that can be stored as a configuration value. Overrides FieldItemInterface::storageSettingsToConfigData 2
FieldItemBase::view public function Returns a renderable array for a single field item. Overrides FieldItemInterface::view
FieldItemBase::writePropertyValue protected function Different to the parent Map class, we avoid creating property objects as far as possible in order to optimize performance. Thus we just update $this->values if no property object has been created yet. Overrides Map::writePropertyValue
FieldItemBase::__construct public function Constructs a TypedData object given its definition and context. Overrides TypedData::__construct 1
FieldItemBase::__get public function Magic method: Gets a property value. Overrides FieldItemInterface::__get 2
FieldItemBase::__isset public function Magic method: Determines whether a property is set. Overrides FieldItemInterface::__isset
FieldItemBase::__set public function Magic method: Sets a property value. Overrides FieldItemInterface::__set 1
FieldItemBase::__unset public function Magic method: Unsets a property. Overrides FieldItemInterface::__unset
Map::$definition protected property The data definition. Overrides TypedData::$definition
Map::$properties protected property The array of properties.
Map::$values protected property An array of values for the contained properties.
Map::applyDefaultValue public function Applies the default value. Overrides TypedData::applyDefaultValue 4
Map::get public function Gets a property object. Overrides ComplexDataInterface::get
Map::getIterator public function
Map::getProperties public function Gets an array of property objects. Overrides ComplexDataInterface::getProperties
Map::getString public function Returns a string representation of the data. Overrides TypedData::getString
Map::set public function Sets a property value. Overrides ComplexDataInterface::set
Map::toArray public function Returns an array of all property values. Overrides ComplexDataInterface::toArray 1
Map::__clone public function Magic method: Implements a deep clone.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TypedData::$name protected property The property name.
TypedData::$parent protected property The parent typed data object.
TypedData::createInstance public static function Constructs a TypedData object given its definition and context. Overrides TypedDataInterface::createInstance
TypedData::getDataDefinition public function Gets the data definition. Overrides TypedDataInterface::getDataDefinition
TypedData::getName public function Returns the name of a property or item. Overrides TypedDataInterface::getName
TypedData::getParent public function Returns the parent data structure; i.e. either complex data or a list. Overrides TypedDataInterface::getParent
TypedData::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
TypedData::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
TypedData::getPropertyPath public function Returns the property path of the data. Overrides TypedDataInterface::getPropertyPath
TypedData::getRoot public function Returns the root of the typed data tree. Overrides TypedDataInterface::getRoot
TypedData::setContext public function Sets the context of a property or item via a context aware parent. Overrides TypedDataInterface::setContext
TypedData::validate public function Validates the currently set data value. Overrides TypedDataInterface::validate
TypedDataTrait::$typedDataManager protected property The typed data manager used for creating the data types.
TypedDataTrait::getTypedDataManager public function Gets the typed data manager. 2
TypedDataTrait::setTypedDataManager public function Sets the typed data manager. 2