You are here

public function GroupMembership::postInstall in Group 8

Runs tasks after the group content type for this plugin has been created.

A good example of what you might want to do here, is the installation of extra locked fields on the group content type. You can find an example in \Drupal\group\Plugin\GroupContentEnabler\GroupMembership::postInstall().

Overrides GroupContentEnablerBase::postInstall

File

src/Plugin/GroupContentEnabler/GroupMembership.php, line 93

Class

GroupMembership
Provides a content enabler for users.

Namespace

Drupal\group\Plugin\GroupContentEnabler

Code

public function postInstall() {

  // Only create config objects while config import is not in progress.
  if (!\Drupal::isConfigSyncing()) {
    $group_content_type_id = $this
      ->getContentTypeConfigId();

    // 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.
    FieldConfig::create([
      'field_storage' => FieldStorageConfig::loadByName('group_content', 'group_roles'),
      'bundle' => $group_content_type_id,
      'label' => $this
        ->t('Roles'),
      'settings' => [
        'handler' => 'group_type:group_role',
        'handler_settings' => [
          'group_type_id' => $this
            ->getGroupTypeId(),
        ],
      ],
    ])
      ->save();

    // 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 = EntityFormDisplay::load($default_display_id))) {
      $form_display = EntityFormDisplay::create([
        'targetEntityType' => 'group_content',
        'bundle' => $group_content_type_id,
        'mode' => 'default',
        'status' => TRUE,
      ]);
    }

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

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

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