muser-8.x-1.x-dev/modules/custom/muser_project/src/Plugin/Action/InactivateAction.php
modules/custom/muser_project/src/Plugin/Action/InactivateAction.php
<?php
namespace Drupal\muser_project\Plugin\Action;
use Drupal\views_bulk_operations\Action\ViewsBulkOperationsActionBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* Action to inactivate a project for a/the current round.
*
* @Action(
* id = "muser_project_inactivate_project",
* label = @Translation("Inactivate this project in current round."),
* type = "node",
* confirm = TRUE,
* )
*/
class InactivateAction extends ViewsBulkOperationsActionBase {
use StringTranslationTrait;
/**
* {@inheritdoc}
*/
public function execute($entity = NULL) {
// Current Round
$round = muser_project_get_current_round();
if (!$round) {
return $this->t('There is no current round.');
}
$pr = muser_project_get_project_round_for_project($entity->id(), $round);
if (!$pr) {
return $this->t('@label is not in the current round.', ['@label' => $entity->label()]);
}
if (!muser_project_remove_project_from_round($pr)) {
return $this->t('Problem removing @label from the current round.', ['@label' => $entity->label()]);
}
// Don't return anything for a default completion message, otherwise return translatable markup.
return $this->t('Project inactivated in current round.');
}
/**
* {@inheritdoc}
*/
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
// Check if this is a project entity.
if ($object->getEntityType()->id() === 'node' && $object->bundle() == 'project') {
// Check if the user as edit access to the project
$access = $object->access('update', $account, TRUE);
// Check secondary permissions
if ($access->isAllowed()) {
if (!$account->hasPermission('administer project rounds')) {
// Check if in posting period.
$round_nid = muser_project_get_current_round();
// If not in the posting period then return false
if (!$round_nid || !muser_project_round_in_period($round_nid)) {
return FALSE;
}
}
}
return $return_as_object ? $access : $access->isAllowed();
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function executeMultiple(array $objects) {
$results = parent::executeMultiple($objects);
// Clear the static var that holds the rounds for projects.
drupal_static('muser_project_get_project_round_for_project', [], TRUE);
return $results;
}
}