You are here

protected function FarmFieldFactory::modifyEntityReferenceRevisionsField in farmOS 2.x

Entity reference revisions field modifier.

Parameters

\Drupal\Core\Field\BaseFieldDefinition &$field: A base field definition object.

array $options: An array of options.

1 call to FarmFieldFactory::modifyEntityReferenceRevisionsField()
FarmFieldFactory::buildFieldDefinition in modules/core/field/src/FarmFieldFactory.php
Builds a field definition with farmOS opinions.

File

modules/core/field/src/FarmFieldFactory.php, line 454

Class

FarmFieldFactory
Factory for generating farmOS field definitions.

Namespace

Drupal\farm_field

Code

protected function modifyEntityReferenceRevisionsField(BaseFieldDefinition &$field, array $options = []) {

  // If a target type is not specified, throw an exception.
  if (empty($options['target_type'])) {
    throw new FieldException('No target_type was specified.');
  }

  // Set the target type.
  $field
    ->setSetting('target_type', $options['target_type']);

  // Build additional settings based on the target type.
  switch ($options['target_type']) {

    // Quantity reference.
    case 'quantity':
      $handler = 'default:quantity';
      $handler_settings = [
        'target_bundles' => NULL,
        'sort' => [
          'field' => 'label',
          'direction' => 'asc',
        ],
        'auto_create' => FALSE,
        'auto_create_bundle' => '',
      ];
      $form_display_options = [
        'type' => 'inline_entity_form_complex',
        'settings' => [
          'form_mode' => 'default',
          'revision' => TRUE,
          'override_labels' => FALSE,
          'label_singular' => '',
          'label_plural' => '',
          'collapsible' => FALSE,
          'collapsed' => FALSE,
          'allow_new' => TRUE,
          'allow_existing' => FALSE,
          'match_operator' => 'CONTAINS',
          'allow_duplicate' => FALSE,
        ],
        'weight' => $options['weight']['form'] ?? 0,
      ];
      $view_display_options = [
        'label' => 'inline',
        'type' => 'entity_reference_revisions_entity_view',
        'settings' => [
          'view_mode' => 'default',
          'link' => FALSE,
        ],
        'weight' => $options['weight']['view'] ?? 0,
      ];
      break;

    // Otherwise, throw an exception.
    default:
      throw new FieldException('Unsupported target_type.');
  }

  // Set the handler and handler settings.
  $field
    ->setSetting('handler', $handler);
  $field
    ->setSetting('handler_settings', $handler_settings);

  // Set form and view display options.
  $field
    ->setDisplayOptions('form', $form_display_options);
  $field
    ->setDisplayOptions('view', $view_display_options);
}