You are here

EntityDeleteAction.php in Views Bulk Operations (VBO) 8.3

File

src/Plugin/Action/EntityDeleteAction.php
View source
<?php

namespace Drupal\views_bulk_operations\Plugin\Action;

use Drupal\views_bulk_operations\Action\ViewsBulkOperationsActionBase;
use Drupal\Core\Entity\TranslatableInterface;
use Drupal\Core\Session\AccountInterface;

/**
 * Delete entity action with default confirmation form.
 *
 * @Action(
 *   id = "views_bulk_operations_delete_entity",
 *   label = @Translation("Delete selected entities / translations"),
 *   type = "",
 *   confirm = TRUE,
 * )
 */
class EntityDeleteAction extends ViewsBulkOperationsActionBase {

  /**
   * {@inheritdoc}
   */
  public function execute($entity = NULL) {
    if ($entity instanceof TranslatableInterface && !$entity
      ->isDefaultTranslation()) {
      $untranslated_entity = $entity
        ->getUntranslated();
      $untranslated_entity
        ->removeTranslation($entity
        ->language()
        ->getId());
      $untranslated_entity
        ->save();
      return $this
        ->t('Delete translations');
    }
    else {
      $entity
        ->delete();
      return $this
        ->t('Delete entities');
    }
  }

  /**
   * {@inheritdoc}
   */
  public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
    $access = $object
      ->access('delete', $account, TRUE);
    if ($object
      ->getEntityType() === 'node') {
      $access
        ->andIf($object->status
        ->access('delete', $account, TRUE));
    }
    return $return_as_object ? $access : $access
      ->isAllowed();
  }

}

Classes

Namesort descending Description
EntityDeleteAction Delete entity action with default confirmation form.