You are here

public static function Og::createField in Organic groups 8

Create an organic groups field in a bundle.

Parameters

string $plugin_id: The OG field plugin ID, which is also the default field name.

string $entity_type: The entity type.

string $bundle: The bundle name.

array $settings: (Optional) allow overriding the default definitions of the field storage config and field config. Allowed values:

  • field_storage_config: Array with values to override the field storage config definitions. Values should comply with FieldStorageConfig::create().
  • field_config: Array with values to override the field config definitions. Values should comply with FieldConfig::create()
  • form_display: Array with values to override the form display definitions.
  • view_display: Array with values to override the view display definitions.

Return value

\Drupal\Core\Field\FieldConfigInterface The created or existing field config.

20 calls to Og::createField()
AccessByOgMembershipTest::setUp in tests/src/Kernel/Access/AccessByOgMembershipTest.php
CacheInvalidationOnGroupChangeTest::setUp in tests/src/Kernel/Entity/CacheInvalidationOnGroupChangeTest.php
EntityCreateAccessTest::setUp in tests/src/Kernel/Entity/EntityCreateAccessTest.php
GetBundleByBundleTest::testGetBundleIdsByBundle in tests/src/Kernel/Entity/GetBundleByBundleTest.php
Tests retrieval of bundles that are referenc[ed|ing] bundles.
GetGroupContentTest::testBasicGroupReferences in tests/src/Kernel/Entity/GetGroupContentTest.php
Test retrieval of group content that references a single group.

... See full list

File

src/Og.php, line 52

Class

Og
A static helper class for OG.

Namespace

Drupal\og

Code

public static function createField($plugin_id, $entity_type, $bundle, array $settings = []) {
  $settings = $settings + [
    'field_storage_config' => [],
    'field_config' => [],
    'form_display' => [],
    'view_display' => [],
  ];
  $field_name = !empty($settings['field_name']) ? $settings['field_name'] : $plugin_id;

  // Get the field definition and add the entity info to it. By doing so
  // we validate the the field can be attached to the entity. For example,
  // the OG access module's field can be attached only to node entities, so
  // any other entity will throw an exception.

  /** @var \Drupal\og\OgFieldBase $og_field */
  $og_field = static::getFieldBaseDefinition($plugin_id)
    ->setFieldName($field_name)
    ->setBundle($bundle)
    ->setEntityType($entity_type);
  if (!FieldStorageConfig::loadByName($entity_type, $field_name)) {
    $field_storage_config = NestedArray::mergeDeep($og_field
      ->getFieldStorageBaseDefinition(), $settings['field_storage_config']);
    FieldStorageConfig::create($field_storage_config)
      ->save();
  }
  if (!($field_definition = FieldConfig::loadByName($entity_type, $bundle, $field_name))) {
    $field_config = NestedArray::mergeDeep($og_field
      ->getFieldBaseDefinition(), $settings['field_config']);
    $field_definition = FieldConfig::create($field_config);
    $field_definition
      ->save();

    // @todo Verify this is still needed here.
    static::invalidateCache();
  }

  // Make the field visible in the default form display.

  /** @var EntityFormDisplayInterface $form_display */
  $form_display = \Drupal::entityTypeManager()
    ->getStorage('entity_form_display')
    ->load("{$entity_type}.{$bundle}.default");

  // If not found, create a fresh form display object. This is by design,
  // configuration entries are only created when an entity form display is
  // explicitly configured and saved.
  if (!$form_display) {
    $form_display = \Drupal::entityTypeManager()
      ->getStorage('entity_form_display')
      ->create([
      'targetEntityType' => $entity_type,
      'bundle' => $bundle,
      'mode' => 'default',
      'status' => TRUE,
    ]);
  }
  $form_display_definition = $og_field
    ->getFormDisplayDefinition($settings['form_display']);
  $form_display
    ->setComponent($plugin_id, $form_display_definition);
  $form_display
    ->save();

  // Set the view display for the "default" view display.
  $view_display_definition = $og_field
    ->getViewDisplayDefinition($settings['view_display']);

  /** @var EntityDisplayInterface $view_display */
  $view_display = \Drupal::entityTypeManager()
    ->getStorage('entity_view_display')
    ->load("{$entity_type}.{$bundle}.default");
  if (!$view_display) {
    $view_display = \Drupal::entityTypeManager()
      ->getStorage('entity_view_display')
      ->create([
      'targetEntityType' => $entity_type,
      'bundle' => $bundle,
      'mode' => 'default',
      'status' => TRUE,
    ]);
  }
  $view_display
    ->setComponent($plugin_id, $view_display_definition);
  $view_display
    ->save();

  // Refresh the group manager data, we have added a group type.
  static::groupTypeManager()
    ->resetGroupRelationMap();
  return $field_definition;
}