View source
<?php
namespace Drupal\groupmedia_vbo\Plugin\Action;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\groupmedia\AttachMediaToGroup;
use Drupal\media\MediaInterface;
use Drupal\views_bulk_operations\Action\ViewsBulkOperationsActionBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class RemoveMediaFromGroup extends ViewsBulkOperationsActionBase implements PluginFormInterface, ContainerFactoryPluginInterface {
protected $entityTypeManager;
protected $attachMediaToGroup;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager, AttachMediaToGroup $attachMediaToGroup) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entityTypeManager;
$this->attachMediaToGroup = $attachMediaToGroup;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('groupmedia.attach_group'));
}
public function execute(MediaInterface $media = NULL) {
$plugin_id = 'group_media:' . $media
->bundle();
$group_content_types = $this->entityTypeManager
->getStorage('group_content_type')
->loadByContentPluginId($plugin_id);
if (empty($group_content_types)) {
return;
}
$group_contents = $this->entityTypeManager
->getStorage('group_content')
->loadByProperties([
'type' => array_keys($group_content_types),
'entity_id' => $media
->id(),
'gid' => $this->configuration['group_id'],
]);
foreach ($group_contents as $group_content) {
$group_content
->delete();
}
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['group_id'] = [
'#title' => $this
->t('Remove from Group'),
'#type' => 'entity_autocomplete',
'#target_type' => 'group',
'#required' => TRUE,
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['group_id'] = $form_state
->getValue('group_id');
}
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
$result = $object
->access('update', $account, TRUE);
return $return_as_object ? $result : $result
->isAllowed();
}
}