You are here

protected function FarmFieldFactory::modifyEntityReferenceField in farmOS 2.x

Entity reference field modifier.

Parameters

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

array $options: An array of options.

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

File

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

Class

FarmFieldFactory
Factory for generating farmOS field definitions.

Namespace

Drupal\farm_field

Code

protected function modifyEntityReferenceField(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']) {

    // Asset reference.
    case 'asset':
      if (!empty($options['target_bundle'])) {
        $handler = 'default:asset';
        $handler_settings = [
          'target_bundles' => [
            $options['target_bundle'] => $options['target_bundle'],
          ],
          'sort' => [
            'field' => '_none',
          ],
          'auto_create' => FALSE,
          'auto_create_bundle' => '',
        ];
      }
      else {
        $handler = 'views';
        $handler_settings = [
          'view' => [
            'view_name' => 'farm_asset_reference',
            'display_name' => 'entity_reference',
          ],
        ];
      }
      $form_display_options = [
        'type' => 'entity_reference_autocomplete',
        'weight' => $options['weight']['form'] ?? 0,
        'settings' => [
          'match_operator' => 'CONTAINS',
          'match_limit' => '10',
          'size' => '60',
          'placeholder' => '',
        ],
      ];
      $view_display_options = [
        'label' => 'inline',
        'type' => 'entity_reference_label',
        'weight' => $options['weight']['view'] ?? 0,
        'settings' => [
          'link' => TRUE,
        ],
      ];
      break;

    // Log.
    case 'log':
      $handler = 'default:log';
      $handler_settings = [
        'target_bundles' => NULL,
        'sort' => [
          'field' => 'name',
          'direction' => 'asc',
        ],
        'auto_create' => FALSE,
        'auto_create_bundle' => '',
      ];
      $form_display_options = [
        'type' => 'entity_reference_autocomplete',
        'weight' => $options['weight']['form'] ?? 0,
      ];
      $view_display_options = [
        'label' => 'inline',
        'type' => 'entity_reference_label',
        'weight' => $options['weight']['view'] ?? 0,
        'settings' => [
          'link' => FALSE,
        ],
      ];
      break;

    // Term reference.
    case 'taxonomy_term':
      $handler = 'default:taxonomy_term';
      $handler_settings = [
        'target_bundles' => [
          $options['target_bundle'] => $options['target_bundle'],
        ],
        'sort' => [
          'field' => 'name',
          'direction' => 'asc',
        ],
        'auto_create' => FALSE,
        'auto_create_bundle' => '',
      ];

      // Auto create term reference if auto_create is enabled.
      if (!empty($options['auto_create'])) {
        $handler_settings['auto_create'] = TRUE;
        $handler_settings['auto_create_bundle'] = $options['target_bundle'];
      }
      $form_display_options = [
        'type' => 'entity_reference_autocomplete',
        'weight' => $options['weight']['form'] ?? 0,
      ];
      $view_display_options = [
        'label' => 'inline',
        'type' => 'entity_reference_label',
        'weight' => $options['weight']['view'] ?? 0,
        'settings' => [
          'link' => FALSE,
        ],
      ];
      break;

    // User reference.
    case 'user':
      $handler = 'default:user';
      $handler_settings = [
        'include_anonymous' => FALSE,
        'filter' => [
          'type' => '_none',
        ],
        'target_bundles' => NULL,
        'sort' => [
          'field' => '_none',
        ],
        'auto_create' => FALSE,
      ];
      $form_display_options = [
        'type' => 'options_select',
        'weight' => $options['weight']['form'] ?? 0,
      ];
      $view_display_options = [
        'label' => 'inline',
        'type' => 'entity_reference_label',
        'weight' => $options['weight']['view'] ?? 0,
        'settings' => [
          'link' => TRUE,
        ],
      ];
      break;

    // Data stream reference.
    case 'data_stream':
      $handler = 'default:data_stream';
      $handler_settings = [
        'filter' => [
          'type' => '_none',
        ],
        'target_bundles' => NULL,
        'sort' => [
          'field' => '_none',
        ],
        'auto_create' => FALSE,
      ];
      $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_label',
        'weight' => $options['weight']['view'] ?? 0,
        'settings' => [
          'link' => TRUE,
        ],
      ];
      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);
}