class GroupMembershipPostInstall in Group 2.0.x
Provides post install tasks for the group_membership relation plugin.
Hierarchy
- class \Drupal\group\Plugin\Group\RelationHandler\GroupMembershipPostInstall implements PostInstallInterface uses StringTranslationTrait, PostInstallTrait
Expanded class hierarchy of GroupMembershipPostInstall
1 string reference to 'GroupMembershipPostInstall'
1 service uses GroupMembershipPostInstall
File
- src/
Plugin/ Group/ RelationHandler/ GroupMembershipPostInstall.php, line 13
Namespace
Drupal\group\Plugin\Group\RelationHandlerView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
GroupMembershipPostInstall:: |
public | function |
Retrieves the tasks to run after plugin installation. Overrides PostInstallTrait:: |
|
GroupMembershipPostInstall:: |
public | function | Installs the group_roles field. | |
GroupMembershipPostInstall:: |
public | function | Constructs a new GroupMembershipPostInstall. | |
RelationHandlerTrait:: |
protected | property | The plugin definition. | |
RelationHandlerTrait:: |
protected | property | The entity type manager. | |
RelationHandlerTrait:: |
protected | property | The group relation manager. | |
RelationHandlerTrait:: |
protected | property | The parent relation handler in the decorator chain. | |
RelationHandlerTrait:: |
protected | property | The plugin ID as read from the definition. | |
RelationHandlerTrait:: |
protected | function | Gets the entity type manager service. | |
RelationHandlerTrait:: |
protected | function | Gets the group relation manager service. | |
RelationHandlerTrait:: |
public | function | ||
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |