You are here

class DeleteEntity in Business Rules 8

Same name and namespace in other branches
  1. 2.x src/Plugin/BusinessRulesAction/DeleteEntity.php \Drupal\business_rules\Plugin\BusinessRulesAction\DeleteEntity

Class DeleteEntity.

@package Drupal\business_rules\Plugin\BusinessRulesAction

Plugin annotation


@BusinessRulesAction(
  id = "delete_entity",
  label = @Translation("Delete an entity"),
  group = @Translation("Entity"),
  description = @Translation("Delete an entity."),
  isContextDependent = FALSE,
  hasTargetEntity = TRUE,
  hasTargetBundle = TRUE,
  hasTargetField = TRUE,
)

Hierarchy

Expanded class hierarchy of DeleteEntity

File

src/Plugin/BusinessRulesAction/DeleteEntity.php, line 27

Namespace

Drupal\business_rules\Plugin\BusinessRulesAction
View source
class DeleteEntity extends BusinessRulesActionPlugin {

  /**
   * {@inheritdoc}
   */
  public function getSettingsForm(array &$form, FormStateInterface $form_state, ItemInterface $item) {
    $settings['value'] = [
      '#type' => 'textfield',
      '#title' => t('Value'),
      '#required' => TRUE,
      '#default_value' => $item
        ->getSettings('value'),
      '#description' => t('The value to be compared against the field.
        <br>All entities which the field equals to this value will be deleted.'),
    ];
    return $settings;
  }

  /**
   * {@inheritdoc}
   */
  public function execute(ActionInterface $action, BusinessRulesEvent $event) {

    // Get settings.
    $entity_type = $action
      ->getTargetEntityType();
    $bundle = $action
      ->getTargetBundle();
    $field = $action
      ->getSettings('field');
    $value = $action
      ->getSettings('value');
    $value = $this
      ->processVariables($value, $event
      ->getArgument('variables'));

    // Load entities ids to delete.

    /** @var \Drupal\Core\Entity\Query\QueryInterface $query */
    $query = \Drupal::getContainer()
      ->get('entity_type.manager')
      ->getStorage($entity_type)
      ->getQuery()
      ->condition($field, $value);
    $ids = $query
      ->execute();

    // Delete entities.

    /** @var \Drupal\Core\Entity\EntityStorageInterface $entityManager */
    $entityManager = \Drupal::entityTypeManager()
      ->getStorage($entity_type);
    $entities = $entityManager
      ->loadMultiple($ids);
    foreach ($entities as $key => $entity) {
      if ($entity
        ->bundle() != $bundle) {
        unset($entities[$key]);
      }
    }
    $entityManager
      ->delete($entities);
    $result = [
      '#type' => 'markup',
      '#markup' => t('Entity: %entity, Bundle: %bundle, Id(s): (%id) has been deleted.', [
        '%entity' => $entity_type,
        '%bundle' => $bundle,
        '%id' => implode(',', $ids),
      ]),
    ];
    return $result;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BusinessRulesItemPluginBase::$processor protected property The business rules processor.
BusinessRulesItemPluginBase::$util protected property The business rules util.
BusinessRulesItemPluginBase::buildForm public function Form constructor. Overrides BusinessRulesItemPluginInterface::buildForm 11
BusinessRulesItemPluginBase::getDescription public function Provide a description of the item. Overrides BusinessRulesItemPluginInterface::getDescription
BusinessRulesItemPluginBase::getEditUrl public function Get the redirect url for the item edit-form route. Overrides BusinessRulesItemPluginInterface::getEditUrl
BusinessRulesItemPluginBase::getGroup public function Provide the group of the item. Overrides BusinessRulesItemPluginInterface::getGroup
BusinessRulesItemPluginBase::getRedirectUrl public function Get the redirect url for the item collection route. Overrides BusinessRulesItemPluginInterface::getRedirectUrl
BusinessRulesItemPluginBase::getVariables public function Return a variable set with all used variables on the item. Overrides BusinessRulesItemPluginInterface::getVariables 9
BusinessRulesItemPluginBase::pregMatch public function Extract the variables from the plugin settings. Overrides BusinessRulesItemPluginInterface::pregMatch
BusinessRulesItemPluginBase::processSettings public function Process the item settings before it's saved. Overrides BusinessRulesItemPluginInterface::processSettings 19
BusinessRulesItemPluginBase::processTokenArraySetting private function Helper function to process tokens if the setting is an array.
BusinessRulesItemPluginBase::processTokens public function Process the tokens on the settings property for the item. Overrides BusinessRulesItemPluginInterface::processTokens
BusinessRulesItemPluginBase::processVariables public function Process the item replacing the variables by it's values. Overrides BusinessRulesItemPluginInterface::processVariables 1
BusinessRulesItemPluginBase::validateForm public function Plugin form validator. Overrides BusinessRulesItemPluginInterface::validateForm 11
BusinessRulesItemPluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 11
BusinessRulesItemPluginInterface::VARIABLE_REGEX constant
DeleteEntity::execute public function Execute the action. Overrides BusinessRulesActionPlugin::execute
DeleteEntity::getSettingsForm public function Return the form array. Overrides BusinessRulesItemPluginBase::getSettingsForm
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.