You are here

class CheckViewResultCount in Business Rules 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/BusinessRulesCondition/CheckViewResultCount.php \Drupal\business_rules\Plugin\BusinessRulesCondition\CheckViewResultCount

Class CheckViewResultCount.

@package Drupal\business_rules\Plugin\BusinessRulesCondition

Plugin annotation


@BusinessRulesCondition(
  id = "check_views_result_count",
  label = @Translation("Check the number of results returned by a view"),
  group = @Translation("Views"),
  description = @Translation("Compare the number of results returned by a view against a defined number."),
  isContextDependent = FALSE,
  reactsOnIds = {},
  hasTargetEntity = FALSE,
  hasTargetBundle = FALSE,
  hasTargetField = FALSE,
)

Hierarchy

Expanded class hierarchy of CheckViewResultCount

File

src/Plugin/BusinessRulesCondition/CheckViewResultCount.php, line 29

Namespace

Drupal\business_rules\Plugin\BusinessRulesCondition
View source
class CheckViewResultCount extends BusinessRulesConditionPlugin {

  /**
   * {@inheritdoc}
   */
  public function getSettingsForm(array &$form, FormStateInterface $form_state, ItemInterface $item) {
    $settings['view'] = [
      '#type' => 'select',
      '#title' => t('View to execute. View name : Display mode id : Display mode title.'),
      '#options' => $this->util
        ->getViewsOptions(),
      '#required' => TRUE,
      '#default_value' => $item
        ->getSettings('view'),
      '#description' => t('Select the view to compare the number of results.'),
    ];
    $settings['arguments'] = [
      '#type' => 'textarea',
      '#title' => t('Arguments'),
      '#description' => t('Any argument the view may need, one per line. Be aware of including them at same order as the CONTEXTUAL FILTERS configured in the view. You may use variables.'),
      '#default_value' => $item
        ->getSettings('arguments'),
    ];
    $settings['comparison'] = [
      '#type' => 'number',
      '#title' => t('Minimum number of results'),
      '#description' => t('The condition will return true if the view has at least the given number of results.'),
      '#min' => 0,
      '#default_value' => $item
        ->getSettings('comparison'),
    ];
    return $settings;
  }

  /**
   * {@inheritdoc}
   */
  public function process(ConditionInterface $condition, BusinessRulesEvent $event) {

    // Get settings.
    $defined_view = $condition
      ->getSettings('view');
    $args = $condition
      ->getSettings('arguments');
    $comparison = $condition
      ->getSettings('comparison');
    $event_variables = $event
      ->getArgument('variables');

    // Process settings.
    $defined_view = explode(':', $defined_view);
    $view_id = $defined_view[0];
    $display = $defined_view[1];
    $args = explode(chr(10), $args);
    $args = array_map('trim', $args);
    $args = array_filter($args, 'strlen');

    // Process variables.
    foreach ($args as $key => $value) {
      $args[$key] = $this
        ->processVariables($value, $event_variables);
    }

    // Execute view.
    $view = Views::getView($view_id);
    $view
      ->setArguments($args);
    $view
      ->setDisplay($display);
    $view
      ->preExecute();
    $view
      ->build();
    if ($view
      ->execute()) {
      $view_result = $view->result;
      $result = count($view_result);
    }
    else {
      $result = 0;
    }

    // Check the condition.
    if ($result >= $comparison) {
      return TRUE;
    }
    else {
      return FALSE;
    }
  }

}

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
CheckViewResultCount::getSettingsForm public function Return the form array. Overrides BusinessRulesItemPluginBase::getSettingsForm
CheckViewResultCount::process public function Process the condition. Overrides BusinessRulesConditionPlugin::process
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 2
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.