You are here

function domain_access_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.

array $text: The text to use for the field. Keys are: 'name' -- the lower-case, human-readable name of the entity. 'label' -- the form label for the all affiliates field. 'description' -- the help text for the all affiliates field.

If calling this function for entities other than user or node, it is the caller's responsibility to provide this text.

This function is here for convenience during installation. It is not really an API function. Modules wishing to add fields to non-node entities must provide their own field storage. See the field storage YML sample in tests/modules/domain_access_test for an example of field storage definitions.

See also

domain_access_node_type_insert()

domain_access_install()

5 calls to domain_access_confirm_fields()
DomainAccessEntityFieldTest::testDomainAccessEntityFields in domain_access/tests/src/Functional/DomainAccessEntityFieldTest.php
Tests that the fields are accessed properly.
DomainAccessFieldTest::testDomainAccessFields in domain_access/tests/src/Functional/DomainAccessFieldTest.php
Tests that the fields are accessed properly.
domain_access_entity_form_display_insert in domain_access/domain_access.module
Implements hook_ENTITY_TYPE_insert().
domain_access_install in domain_access/domain_access.install
Implements hook_install().
domain_access_node_type_insert in domain_access/domain_access.module
Implements hook_ENTITY_TYPE_insert().

File

domain_access/domain_access.module, line 535
Domain-based access control for content.

Code

function domain_access_confirm_fields($entity_type, $bundle, array $text = []) {

  // We have reports that importing config causes this function to fail.
  try {
    $text['node'] = [
      'name' => 'content',
      'label' => 'Send to all affiliates',
      'description' => 'Make this content available on all domains.',
    ];
    $text['user'] = [
      'name' => 'user',
      'label' => 'Editor for all affiliates',
      'description' => 'Make this user an editor on all domains.',
    ];
    $id = $entity_type . '.' . $bundle . '.' . DomainAccessManagerInterface::DOMAIN_ACCESS_FIELD;
    $field_storage = \Drupal::entityTypeManager()
      ->getStorage('field_config');
    if (!($field = $field_storage
      ->load($id))) {
      $field = [
        'field_name' => DomainAccessManagerInterface::DOMAIN_ACCESS_FIELD,
        'entity_type' => $entity_type,
        'label' => 'Domain Access',
        'bundle' => $bundle,
        // Users should not be required to be a domain editor.
        'required' => $entity_type !== 'user',
        'description' => 'Select the affiliate domain(s) for this ' . $text[$entity_type]['name'],
        'default_value_callback' => 'Drupal\\domain_access\\DomainAccessManager::getDefaultValue',
        '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_storage
        ->create($field);
      $field_config
        ->save();
    }

    // Assign the all affiliates field.
    $id = $entity_type . '.' . $bundle . '.' . DomainAccessManagerInterface::DOMAIN_ACCESS_ALL_FIELD;
    if (!($field = $field_storage
      ->load($id))) {
      $field = [
        'field_name' => DomainAccessManagerInterface::DOMAIN_ACCESS_ALL_FIELD,
        'entity_type' => $entity_type,
        'label' => $text[$entity_type]['label'],
        'bundle' => $bundle,
        'required' => FALSE,
        'description' => $text[$entity_type]['description'],
      ];
      $field_config = $field_storage
        ->create($field);
      $field_config
        ->save();
    }

    // Tell the form system how to behave. Default to radio buttons.
    if ($display = \Drupal::entityTypeManager()
      ->getStorage('entity_form_display')
      ->load($entity_type . '.' . $bundle . '.default')) {
      $display
        ->setComponent(DomainAccessManagerInterface::DOMAIN_ACCESS_FIELD, [
        'type' => 'options_buttons',
        'weight' => 40,
      ])
        ->setComponent(DomainAccessManagerInterface::DOMAIN_ACCESS_ALL_FIELD, [
        'type' => 'boolean_checkbox',
        'settings' => [
          'display_label' => 1,
        ],
        'weight' => 41,
      ])
        ->save();
    }
  } catch (Exception $e) {
    \Drupal::logger('domain_access')
      ->notice('Field installation failed.');
  }
}