You are here

protected function ExifSettingsController::addReferenceToEntityType in Exif 8

Same name and namespace in other branches
  1. 8.2 src/Controller/ExifSettingsController.php \Drupal\exif\Controller\ExifSettingsController::addReferenceToEntityType()

Add a field that reference a vocabulary.

Parameters

string $entity_type: The entity type name to be modified.

\Drupal\Core\Entity\EntityInterface $type: The definition of type.

string $fieldLabel: Field description (what is show in forms).

string $fieldName: Field name (the real one used internally).

string $fieldType: Name of the field type to be added.

string $fieldTypeBundle: Name of the bundle to be used.

string $fieldWidget: Name of the widget to use.

array $widgetSettings: Settings to set for the widget.

array $settings: Specific setting for the field (optional).

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

1 call to ExifSettingsController::addReferenceToEntityType()
ExifSettingsController::addFields in src/Controller/ExifSettingsController.php
Add a field to a entity type.

File

src/Controller/ExifSettingsController.php, line 492

Class

ExifSettingsController
Class ExifSettingsController manage action of settings pages.

Namespace

Drupal\exif\Controller

Code

protected function addReferenceToEntityType($entity_type, EntityInterface $type, $fieldLabel, $fieldName, $fieldType, $fieldTypeBundle, $fieldWidget, array $widgetSettings = [], array $settings = []) {
  $realFieldName = 'field_' . $fieldName;
  $storage = $this
    ->getFieldStorageConfig();
  $field_storage = $storage
    ->load($entity_type . '.' . $realFieldName);
  if (empty($field_storage)) {
    $field_storage_values = [
      'field_name' => $realFieldName,
      'field_label' => $fieldLabel,
      'entity_type' => $entity_type,
      'bundle' => $type
        ->id(),
      'type' => 'entity_reference',
      'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
      'translatable' => FALSE,
      'settings' => [
        'target_type' => $fieldType,
      ],
    ];
    $temp = $storage
      ->create($field_storage_values);
    $temp
      ->save();
  }
  $fieldSettings = [
    'handler' => 'default:' . $fieldType,
    'handler_settings' => [
      'target_bundles' => [
        $fieldTypeBundle => $fieldTypeBundle,
      ],
    ],
    'sort' => [
      'field' => '_none',
    ],
    'auto_create' => FALSE,
    'auto_create_bundle' => '',
  ];
  $this
    ->entityAddExtraField($entity_type, $type, $realFieldName, $fieldLabel, $fieldSettings, $fieldWidget, $widgetSettings);
}