You are here

public function DomainEntityMapper::addDomainField in Domain Access Entity 8

Creates domain fields.

Parameters

string $entity_type: The entity type machine name.

string $bundle: The entity type's bundle.

File

src/DomainEntityMapper.php, line 136

Class

DomainEntityMapper
Provides fields operations for domain entity module fields.

Namespace

Drupal\domain_entity

Code

public function addDomainField($entity_type, $bundle) {
  $field_storage = $this
    ->createFieldStorage($entity_type);
  $field = FieldConfig::loadByName($entity_type, $bundle, self::FIELD_NAME);
  if (empty($field)) {
    $field = [
      'label' => 'Domain Access',
      // @Todo Add better naming for entities without bundles.
      'description' => 'Select the affiliate domain(s). If nothing was selected: Affiliated to all domains.',
      'bundle' => $bundle,
      'required' => FALSE,
      'field_storage' => $field_storage,
      'default_value_callback' => 'domain_entity_field_default_domains',
    ];
    $field = $this->entityTypeManager
      ->getStorage('field_config')
      ->create($field);
    $field
      ->save();

    // Assign widget settings for the 'default' form mode.
    $entity_form_display = $this->entityTypeManager
      ->getStorage('entity_form_display')
      ->load($entity_type . '.' . $bundle . '.default');
    if ($entity_form_display) {

      /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $entity_form_display */
      $entity_form_display
        ->setComponent(self::FIELD_NAME, [
        'type' => 'options_buttons',
      ])
        ->save();
    }

    // Assign display settings for the 'default' view mode.
    $entity_view_display = $this->entityTypeManager
      ->getStorage('entity_view_display')
      ->load($entity_type . '.' . $bundle . '.default');
    if ($entity_view_display) {

      /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $entity_view_display */
      $entity_view_display
        ->removeComponent(self::FIELD_NAME)
        ->save();
    }
  }
}