You are here

function og_og_role_insert in Organic groups 8

Implements hook_ENTITY_TYPE_insert() for OgRole entities.

File

./og.module, line 381

Code

function og_og_role_insert(OgRoleInterface $role) {

  // Create actions to add or remove the role, except for the required default
  // roles 'member' and 'non-member'. These cannot be added or removed.
  if ($role
    ->getRoleType() === OgRoleInterface::ROLE_TYPE_REQUIRED) {
    return;
  }

  // Skip creation of action plugins while config import is in progress.
  if ($role
    ->isSyncing()) {
    return;
  }
  $add_id = 'og_membership_add_single_role_action.' . $role
    ->getName();
  if (!Action::load($add_id)) {
    $action = Action::create([
      'id' => $add_id,
      'type' => 'og_membership',
      'label' => new TranslatableMarkup('Add the @label role to the selected members', [
        '@label' => $role
          ->getName(),
      ]),
      'configuration' => [
        'role_name' => $role
          ->getName(),
      ],
      'plugin' => 'og_membership_add_single_role_action',
    ]);
    $action
      ->trustData()
      ->save();
  }
  $remove_id = 'og_membership_remove_single_role_action.' . $role
    ->getName();
  if (!Action::load($remove_id)) {
    $action = Action::create([
      'id' => $remove_id,
      'type' => 'og_membership',
      'label' => new TranslatableMarkup('Remove the @label role from the selected members', [
        '@label' => $role
          ->getName(),
      ]),
      'configuration' => [
        'role_name' => $role
          ->getName(),
      ],
      'plugin' => 'og_membership_remove_single_role_action',
    ]);
    $action
      ->trustData()
      ->save();
  }
}