og-8.x-1.x-dev/src/Plugin/Action/DeleteOgMembership.php
src/Plugin/Action/DeleteOgMembership.php
<?php
declare(strict_types=1);
namespace Drupal\og\Plugin\Action;
use Drupal\Core\Action\ActionBase;
use Drupal\Core\Action\Attribute\Action;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\og\Entity\OgMembership;
use Drupal\og\OgAccessInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Deletes a group membership.
*/
#[Action(
id: 'og_membership_delete_action',
label: new TranslatableMarkup('Delete the selected membership(s)'),
type: 'og_membership',
)]
class DeleteOgMembership extends ActionBase implements ContainerFactoryPluginInterface {
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
protected readonly OgAccessInterface $ogAccess,
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('og.access')
);
}
/**
* {@inheritdoc}
*/
public function execute(?OgMembership $membership = NULL) {
if (!$membership) {
return;
}
$membership->delete();
}
/**
* {@inheritdoc}
*/
public function access($object, ?AccountInterface $account = NULL, $return_as_object = FALSE) {
/** @var \Drupal\og\Entity\OgMembership $object */
// Grant access if the user can manage members in this group.
$access = $this->ogAccess->userAccess($object->getGroup(), 'manage members', $account);
return $return_as_object ? $access : $access->isAllowed();
}
}
