You are here

function domain_source_confirm_fields in Domain Access 8

Creates our fields for an entity bundle.

Parameters

string $entity_type: The entity type being created. Node and user are supported.

string $bundle: The bundle being created.

See also

domain_source_node_type_insert()

domain_source_install()

3 calls to domain_source_confirm_fields()
domain_source_entity_form_display_insert in domain_source/domain_source.module
Implements hook_ENTITY_TYPE_insert().
domain_source_install in domain_source/domain_source.install
Implements hook_install().
domain_source_node_type_insert in domain_source/domain_source.module
Implements hook_ENTITY_TYPE_insert().

File

domain_source/domain_source.module, line 35
Domain-based path rewrites for content.

Code

function domain_source_confirm_fields($entity_type, $bundle) {
  $id = $entity_type . '.' . $bundle . '.' . DomainSourceElementManagerInterface::DOMAIN_SOURCE_FIELD;
  $field_config_storage = \Drupal::entityTypeManager()
    ->getStorage('field_config');
  if (!($field = $field_config_storage
    ->load($id))) {
    $field = [
      'field_name' => DomainSourceElementManagerInterface::DOMAIN_SOURCE_FIELD,
      'entity_type' => $entity_type,
      'label' => 'Domain Source',
      'bundle' => $bundle,
      'required' => FALSE,
      'description' => 'Select the canonical domain for this content.',
      'settings' => [
        'handler' => 'default:domain',
        // Handler_settings are deprecated but seem to be necessary here.
        'handler_settings' => [
          'target_bundles' => NULL,
          'sort' => [
            'field' => 'weight',
            'direction' => 'ASC',
          ],
        ],
        'target_bundles' => NULL,
        'sort' => [
          'field' => 'weight',
          'direction' => 'ASC',
        ],
      ],
    ];
    $field_config = $field_config_storage
      ->create($field);
    $field_config
      ->save();
  }

  // Tell the form system how to behave. Default to radio buttons.
  $display = \Drupal::entityTypeManager()
    ->getStorage('entity_form_display')
    ->load($entity_type . '.' . $bundle . '.default');
  if ($display) {
    $display
      ->setComponent(DomainSourceElementManagerInterface::DOMAIN_SOURCE_FIELD, [
      'type' => 'options_select',
      'weight' => 42,
    ])
      ->save();
  }
}