You are here

public function GroupMembership::getGroupOperations in Group 8

Provides a list of operations for a group.

These operations can be implemented in numerous ways by extending modules. Out of the box, Group provides a block that shows the available operations to a user visiting a route with a group in its URL.

Do not forget to specify cacheable metadata if you need to. This can be done in ::getGroupOperationsCacheableMetadata().

Parameters

\Drupal\group\Entity\GroupInterface $group: The group to generate the operations for.

Return value

array An associative array of operation links to show when in a group context, keyed by operation name, containing the following key-value pairs:

  • title: The localized title of the operation.
  • url: An instance of \Drupal\Core\Url for the operation URL.
  • weight: The weight of the operation.

Overrides GroupContentEnablerBase::getGroupOperations

See also

::getGroupOperationsCacheableMetadata()

File

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

Class

GroupMembership
Provides a content enabler for users.

Namespace

Drupal\group\Plugin\GroupContentEnabler

Code

public function getGroupOperations(GroupInterface $group) {
  $account = \Drupal::currentUser();
  $operations = [];
  if ($group
    ->getMember($account)) {
    if ($group
      ->hasPermission('leave group', $account)) {
      $operations['group-leave'] = [
        'title' => $this
          ->t('Leave group'),
        'url' => new Url('entity.group.leave', [
          'group' => $group
            ->id(),
        ]),
        'weight' => 99,
      ];
    }
  }
  elseif ($group
    ->hasPermission('join group', $account)) {
    $operations['group-join'] = [
      'title' => $this
        ->t('Join group'),
      'url' => new Url('entity.group.join', [
        'group' => $group
          ->id(),
      ]),
      'weight' => 0,
    ];
  }
  return $operations;
}