You are here

class GroupMembershipPostInstall in Group 2.0.x

Provides post install tasks for the group_membership relation plugin.

Hierarchy

Expanded class hierarchy of GroupMembershipPostInstall

1 string reference to 'GroupMembershipPostInstall'
group.services.yml in ./group.services.yml
group.services.yml
1 service uses GroupMembershipPostInstall
group.relation_handler.post_install.group_membership in ./group.services.yml
Drupal\group\Plugin\Group\RelationHandler\GroupMembershipPostInstall

File

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

Namespace

Drupal\group\Plugin\Group\RelationHandler
View source
class GroupMembershipPostInstall implements PostInstallInterface {
  use PostInstallTrait;
  use StringTranslationTrait;

  /**
   * Constructs a new GroupMembershipPostInstall.
   *
   * @param \Drupal\group\Plugin\Group\RelationHandler\PostInstallInterface $parent
   *   The default post install handler.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation service.
   */
  public function __construct(PostInstallInterface $parent, EntityTypeManagerInterface $entity_type_manager, TranslationInterface $string_translation) {
    $this->parent = $parent;
    $this->entityTypeManager = $entity_type_manager;
    $this->stringTranslation = $string_translation;
  }

  /**
   * {@inheritdoc}
   */
  public function getInstallTasks() {
    $tasks = $this->parent
      ->getInstallTasks();
    $tasks['install-group-roles-field'] = [
      $this,
      'installGroupRolesField',
    ];
    return $tasks;
  }

  /**
   * Installs the group_roles field.
   *
   * @param \Drupal\group\Entity\GroupContentTypeInterface $group_content_type
   *   The GroupContentType created by installing the plugin.
   * @param $is_syncing
   *   Whether config is syncing.
   */
  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,
      ],
    ]));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GroupMembershipPostInstall::getInstallTasks public function Retrieves the tasks to run after plugin installation. Overrides PostInstallTrait::getInstallTasks
GroupMembershipPostInstall::installGroupRolesField public function Installs the group_roles field.
GroupMembershipPostInstall::__construct public function Constructs a new GroupMembershipPostInstall.
RelationHandlerTrait::$definition protected property The plugin definition.
RelationHandlerTrait::$entityTypeManager protected property The entity type manager.
RelationHandlerTrait::$groupRelationManager protected property The group relation manager.
RelationHandlerTrait::$parent protected property The parent relation handler in the decorator chain.
RelationHandlerTrait::$pluginId protected property The plugin ID as read from the definition.
RelationHandlerTrait::entityTypeManager protected function Gets the entity type manager service.
RelationHandlerTrait::groupRelationManager protected function Gets the group relation manager service.
RelationHandlerTrait::init public function
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.