public function ViewResultVariable::variableFields in Business Rules 8
Same name and namespace in other branches
- 2.x src/Plugin/BusinessRulesVariable/ViewResultVariable.php \Drupal\business_rules\Plugin\BusinessRulesVariable\ViewResultVariable::variableFields()
Display the view variable fields.
Parameters
\Drupal\business_rules\Entity\Variable $variable: The variable entity.
Return value
\Drupal\Core\Ajax\AjaxResponse|array The AjaxResponse or the render array.
1 call to ViewResultVariable::variableFields()
- ViewResultVariable::changeDetails in src/
Plugin/ BusinessRulesVariable/ ViewResultVariable.php - Change the variable details box.
File
- src/
Plugin/ BusinessRulesVariable/ ViewResultVariable.php, line 125
Class
- ViewResultVariable
- Class ViewResultVariable.
Namespace
Drupal\business_rules\Plugin\BusinessRulesVariableCode
public function variableFields(Variable $variable) {
// Get settings.
$defined_view = $variable
->getSettings('view');
// Process settings.
$defined_view = explode(':', $defined_view);
$view_id = $defined_view[0];
$display = $defined_view[1];
// Get view fields.
$view = Views::getView($view_id);
$view
->setDisplay($display);
$view
->preExecute();
$view
->build();
$fields = $view->field;
$header = [
'variable' => t('Variable'),
'field' => t('Field'),
'type' => t('Type'),
];
$rows = [];
if (count($fields)) {
foreach ($fields as $field_name => $field) {
$field_id = $field->field;
$definition = $field->definition;
$entity_type = $definition['entity_type'];
if ($field
->getBaseId() == 'field') {
// Need to check in all bundles if the field is available.
$bundles = $this->bundleInfo
->getBundleInfo($entity_type);
$bundles = array_keys($bundles);
$found = FALSE;
$idx_bundle = 0;
while (!$found && $idx_bundle < count($bundles)) {
$bundle = $bundles[$idx_bundle];
// Now, with the bundle info, we can load the fields definitions.
$fields_definitions = $this->entityFieldManager
->getFieldDefinitions($entity_type, $bundle);
$field_definition = isset($fields_definitions[$field_name]) ? $fields_definitions[$field_name] : NULL;
// If we are not in the correct bundle, lets try again.
if (empty($field_definition)) {
$idx_bundle++;
}
else {
$field_type = $field_definition
->getType();
$found = TRUE;
}
}
$rows[] = [
'variable' => [
'data' => [
'#markup' => '{{' . $variable
->id() . '->' . $field_id . '}}',
],
],
'field' => [
'data' => [
'#markup' => $field
->label() ? $field
->label() : $field->realField,
],
],
'type' => [
'data' => [
'#markup' => $field_type,
],
],
];
}
}
}
else {
$rows[] = [
'data' => [
'#markup' => t('This view has no fields.'),
],
'colspan' => 3,
];
}
$content['help'] = [
'#type' => 'markup',
'#markup' => t('Notice that as this items are arrays, you only can use this variable values on items inside an action type: "Loop through a view result variable".'),
];
$content['variable_fields'] = [
'#type' => 'table',
'#rows' => $rows,
'#header' => $header,
'#sticky' => TRUE,
];
return $content;
}