You are here

public function GroupMembershipPostInstall::installGroupRolesField in Group 2.0.x

Installs the group_roles field.

Parameters

\Drupal\group\Entity\GroupContentTypeInterface $group_content_type: The GroupContentType created by installing the plugin.

$is_syncing: Whether config is syncing.

File

src/Plugin/Group/RelationHandler/GroupMembershipPostInstall.php, line 51

Class

GroupMembershipPostInstall
Provides post install tasks for the group_membership relation plugin.

Namespace

Drupal\group\Plugin\Group\RelationHandler

Code

public function installGroupRolesField(GroupContentTypeInterface $group_content_type, $is_syncing) {

  // Only create config objects while config import is not in progress.
  if ($is_syncing === TRUE) {
    return;
  }
  $fc_storage = $this
    ->entityTypeManager()
    ->getStorage('field_config');
  $fsc_storage = $this
    ->entityTypeManager()
    ->getStorage('field_storage_config');
  $efd_storage = $this
    ->entityTypeManager()
    ->getStorage('entity_form_display');
  $evd_storage = $this
    ->entityTypeManager()
    ->getStorage('entity_view_display');

  // Add the group_roles field to the newly added group content type. The
  // field storage for this is defined in the config/install folder. The
  // default handler for 'group_role' target entities in the 'group_type'
  // handler group is GroupTypeRoleSelection.
  $group_content_type_id = $group_content_type
    ->id();
  $field_storage = $fsc_storage
    ->load('group_content.group_roles');
  $field = $fc_storage
    ->load("group_content.{$group_content_type_id}.group_roles");
  if (!empty($field)) {
    throw new \RuntimeException(sprintf('The field group_roles already exists on group content type "%s".', $group_content_type_id));
  }
  $fc_storage
    ->save($fc_storage
    ->create([
    'field_storage' => $field_storage,
    'bundle' => $group_content_type_id,
    'label' => $this
      ->t('Roles'),
    'settings' => [
      'handler' => 'group_type:group_role',
      'handler_settings' => [
        'group_type_id' => $group_content_type
          ->getGroupTypeId(),
      ],
    ],
  ]));

  // Build the 'default' display ID for both the entity form and view mode.
  $default_display_id = "group_content.{$group_content_type_id}.default";

  // Build or retrieve the 'default' form mode.
  if (!($form_display = $efd_storage
    ->load($default_display_id))) {
    $form_display = $efd_storage
      ->create([
      'targetEntityType' => 'group_content',
      'bundle' => $group_content_type_id,
      'mode' => 'default',
      'status' => TRUE,
    ]);
  }

  // Build or retrieve the 'default' view mode.
  if (!($view_display = $evd_storage
    ->load($default_display_id))) {
    $view_display = $evd_storage
      ->create([
      'targetEntityType' => 'group_content',
      'bundle' => $group_content_type_id,
      'mode' => 'default',
      'status' => TRUE,
    ]);
  }

  // Assign widget settings for the 'default' form mode.
  $efd_storage
    ->save($form_display
    ->setComponent('group_roles', [
    'type' => 'options_buttons',
  ]));

  // Assign display settings for the 'default' view mode.
  $evd_storage
    ->save($view_display
    ->setComponent('group_roles', [
    'label' => 'above',
    'type' => 'entity_reference_label',
    'settings' => [
      'link' => 0,
    ],
  ]));
}