Same name and namespace in other branches
  1. 8.x-2.x src/Controller/ExifSettingsController.php \Drupal\exif\Controller\ExifSettingsController::addFieldToEntityType() 1 comment

Add a Field to an Entity Type.

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 $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::addFieldToEntityType()
ExifSettingsController::addFields in src/Controller/ExifSettingsController.php
Add a field to a entity type.

File

src/Controller/ExifSettingsController.php, line 347

Class

ExifSettingsController
Class ExifSettingsController manage action of settings pages.

Namespace

Drupal\exif\Controller

Code

protected function addFieldToEntityType($entity_type, EntityInterface $type, $fieldLabel, $fieldName, $fieldType, $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' => $fieldType,
            'translatable' => FALSE,
        ];
        $storage->create($field_storage_values)
            ->save();
    }
    $fieldSettings = [
        'display_summary' => TRUE,
    ];
    $this->entityAddExtraField($entity_type, $type, $realFieldName, $fieldLabel, $fieldSettings, $fieldWidget, $widgetSettings);
}