You are here

protected function ExifSettingsController::entityAddExtraField in Exif 8

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

Add a new field to the entity type.

Parameters

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

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

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

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

array $fieldSettings: Settings for the field.

string $fieldWidget: Name of the widget to use.

array $widgetSettings: Settings to set for the widget.

Return value

\Drupal\Core\Entity\EntityInterface The Field Entity.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

2 calls to ExifSettingsController::entityAddExtraField()
ExifSettingsController::addFieldToEntityType in src/Controller/ExifSettingsController.php
Add a Field to an Entity Type.
ExifSettingsController::addReferenceToEntityType in src/Controller/ExifSettingsController.php
Add a field that reference a vocabulary.

File

src/Controller/ExifSettingsController.php, line 419

Class

ExifSettingsController
Class ExifSettingsController manage action of settings pages.

Namespace

Drupal\exif\Controller

Code

protected function entityAddExtraField($entity_type, EntityInterface $type, $fieldName, $fieldLabel, array $fieldSettings, $fieldWidget, array $widgetSettings) {
  $machineName = strtolower($fieldName);

  // Add or remove the body field, as needed.
  $storage = $this
    ->getFieldStorageConfig();
  $field_storage = $storage
    ->load($entity_type . '.' . $machineName);
  $field_config = $this
    ->getFieldConfig();
  $field = $field_config
    ->load($entity_type . '.' . $type
    ->id() . '.' . $machineName);
  if (empty($field)) {
    $field = $field_config
      ->create([
      'field_storage' => $field_storage,
      'bundle' => $type
        ->id(),
      'label' => $fieldLabel,
      'settings' => $fieldSettings,
    ]);
    $field
      ->save();
  }

  // Assign widget settings for the 'default' form mode.
  $this
    ->entity_get_form_display($entity_type, $type
    ->id(), 'default')
    ->setComponent($machineName, [
    'type' => $fieldWidget,
    'settings' => $widgetSettings,
  ])
    ->save();

  // Assign display settings for the 'default' and 'teaser' view modes.
  $this
    ->entity_get_display($entity_type, $type
    ->id(), 'default')
    ->setComponent($machineName, [
    'label' => 'hidden',
    'type' => 'text_default',
  ])
    ->save();

  // The teaser view mode is created by the Standard profile and therefore
  // might not exist.
  $view_modes = $this->entityDisplayRepository
    ->getViewModes($entity_type);
  if (isset($view_modes['teaser'])) {
    $this
      ->entity_get_display($entity_type, $type
      ->id(), 'teaser')
      ->setComponent($machineName, [
      'label' => 'hidden',
      'type' => 'text_summary_or_trimmed',
    ])
      ->save();
  }
  return $field;
}