You are here

class EntityEmptyVariable in Business Rules 8

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

Class EntityEmptyVariable.

@package Drupal\business_rules\Plugin\BusinessRulesVariable

Plugin annotation


@BusinessRulesVariable(
  id = "entity_empty_variable",
  label = @Translation("Empty Entity variable"),
  group = @Translation("Entity"),
  description = @Translation("Set an empty variable to be filled with a copy
  of an entity by id."), reactsOnIds = {}, isContextDependent = FALSE,
  hasTargetEntity = TRUE, hasTargetBundle = TRUE, hasTargetField = FALSE,
)

Hierarchy

Expanded class hierarchy of EntityEmptyVariable

File

src/Plugin/BusinessRulesVariable/EntityEmptyVariable.php, line 28

Namespace

Drupal\business_rules\Plugin\BusinessRulesVariable
View source
class EntityEmptyVariable extends BusinessRulesVariablePlugin {

  /**
   * The EntityFieldManager.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  protected $entityFieldManager;

  /**
   * The FieldTypePluginManager.
   *
   * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
   */
  protected $fieldTypePluginManager;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entityFieldManager = $this->util->container
      ->get('entity_field.manager');
    $this->fieldTypePluginManager = $this->util->container
      ->get('plugin.manager.field.field_type');
  }

  /**
   * {@inheritdoc}
   */
  public function getSettingsForm(array &$form, FormStateInterface $form_state, ItemInterface $item) {
    $settings['help'] = [
      '#type' => 'markup',
      '#markup' => t('After this variable is filled, you may refer to each field of this variable as {{variable_id->field_name}}'),
    ];
    return $settings;
  }

  /**
   * {@inheritdoc}
   */
  public function changeDetails(Variable $variable, array &$row) {

    // Show a link to a modal window which all fields from the Entity Variable.
    $content = $this->util
      ->getVariableFieldsModalInfo($variable);
    $keyvalue = $this->util
      ->getKeyValueExpirable('entity_empty_variable');
    $keyvalue
      ->set('variableFields.' . $variable
      ->id(), $content);
    $details_link = Link::createFromRoute(t('Click here to see the entity fields'), 'business_rules.ajax.modal', [
      'method' => 'nojs',
      'title' => t('Entity fields'),
      'collection' => 'entity_empty_variable',
      'key' => 'variableFields.' . $variable
        ->id(),
    ], [
      'attributes' => [
        'class' => [
          'use-ajax',
        ],
      ],
    ])
      ->toString();
    $row['description']['data']['#markup'] .= '<br>' . $details_link;
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array &$form, FormStateInterface $form_state) {
    unset($form['variables']);
  }

  /**
   * {@inheritdoc}
   */
  public function evaluate(Variable $variable, BusinessRulesEvent $event) {

    /** @var \Drupal\Core\Entity\Entity $entity */
    $entity_type = $variable
      ->getTargetEntityType();
    $bundle = $variable
      ->getTargetBundle();

    // Node has entity key = 'type', comment has another entity key.
    // Needs to figure out the best way to get the entity key.
    // TODO review this logic in order to get entity in all situations.
    if ($entity_type == 'node') {
      $entity_key = 'type';
    }
    else {

      // Get entity bundle key.
      $manager = \Drupal::entityTypeManager();
      $entity_type1 = $manager
        ->getDefinition($entity_type);
      $entity_key = $entity_type1
        ->get('entity_keys')['bundle'];
    }
    $entity = \Drupal::entityTypeManager()
      ->getStorage($entity_type)
      ->create([
      $entity_key => $bundle,
    ]);

    // ->create(['type' => $bundle]);.
    $variableObject = new VariableObject($variable
      ->id(), $entity, $variable
      ->getType());
    $variableSet = new VariablesSet();
    $variableSet
      ->append($variableObject);
    $fields = $this->entityFieldManager
      ->getFieldDefinitions($variable
      ->getTargetEntityType(), $variable
      ->getTargetBundle());
    foreach ($fields as $field_name => $field_storage) {
      $variableObject = new VariableObject($variable
        ->id() . '->' . $field_name, $entity
        ->get($field_name)->value, $variable
        ->getType());
      $variableSet
        ->append($variableObject);
    }
    return $variableSet;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BusinessRulesItemPluginBase::$processor protected property The business rules processor.
BusinessRulesItemPluginBase::$util protected property The business rules util.
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
BusinessRulesItemPluginInterface::VARIABLE_REGEX constant
EntityEmptyVariable::$entityFieldManager protected property The EntityFieldManager.
EntityEmptyVariable::$fieldTypePluginManager protected property The FieldTypePluginManager.
EntityEmptyVariable::buildForm public function Form constructor. Overrides BusinessRulesItemPluginBase::buildForm
EntityEmptyVariable::changeDetails public function Change the variable details box. Overrides BusinessRulesVariablePlugin::changeDetails
EntityEmptyVariable::evaluate public function Evaluate the variable. Overrides BusinessRulesVariablePlugin::evaluate
EntityEmptyVariable::getSettingsForm public function Return the form array. Overrides BusinessRulesItemPluginBase::getSettingsForm
EntityEmptyVariable::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides BusinessRulesItemPluginBase::__construct
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.