class EntityEmptyVariable in Business Rules 8
Same name and namespace in other branches
- 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
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\business_rules\Plugin\BusinessRulesItemPluginBase implements BusinessRulesItemPluginInterface
- class \Drupal\business_rules\Plugin\BusinessRulesVariablePlugin implements BusinessRulesVariablePluginInterface
- class \Drupal\business_rules\Plugin\BusinessRulesVariable\EntityEmptyVariable
- class \Drupal\business_rules\Plugin\BusinessRulesVariablePlugin implements BusinessRulesVariablePluginInterface
- class \Drupal\business_rules\Plugin\BusinessRulesItemPluginBase implements BusinessRulesItemPluginInterface
Expanded class hierarchy of EntityEmptyVariable
File
- src/
Plugin/ BusinessRulesVariable/ EntityEmptyVariable.php, line 28
Namespace
Drupal\business_rules\Plugin\BusinessRulesVariableView 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;
}
}