You are here

public function BusinessRulesUtil::getVariableFieldsModalInfo in Business Rules 2.x

Same name and namespace in other branches
  1. 8 src/Util/BusinessRulesUtil.php \Drupal\business_rules\Util\BusinessRulesUtil::getVariableFieldsModalInfo()

Display the entity 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.

File

src/Util/BusinessRulesUtil.php, line 756

Class

BusinessRulesUtil
Class BusinessRulesUtil.

Namespace

Drupal\business_rules\Util

Code

public function getVariableFieldsModalInfo(Variable $variable) {
  $fields = $this->entityFieldManager
    ->getFieldDefinitions($variable
    ->getTargetEntityType(), $variable
    ->getTargetBundle());
  $field_types = $this->fieldTypePluginManager
    ->getDefinitions();
  $header = [
    'variable' => $this
      ->t('Variable'),
    'field' => $this
      ->t('Field'),
    'type' => $this
      ->t('Type'),
  ];
  $rows = [];
  foreach ($fields as $field_name => $field_storage) {
    $field_type = $field_storage
      ->getType();
    $rows[] = [
      'variable' => [
        'data' => [
          '#markup' => '{{' . $variable
            ->id() . '->' . $field_name . '}}',
        ],
      ],
      'field' => [
        'data' => [
          '#markup' => $field_storage
            ->getLabel(),
        ],
      ],
      'type' => [
        'data' => [
          '#markup' => $field_types[$field_type]['label'],
        ],
      ],
    ];
  }
  $content['help'] = [
    '#type' => 'markup',
    '#markup' => $this
      ->t('To access a particular multi-value field such as target id, you can use <code>{{@variable_id[delta]}}</code> where "delta" is the delta value to get a one value or <code>{{@variable_id}}</code> to get an array of values.
        <br>To access a particular multi-value field label you can use <code>{{@variable_id[delta]->label}}</code> where "delta" is the delta value to get one label or <code>{{@variable_id->label}}</code> to get an array of labels.', [
      '@variable_id' => $variable
        ->id(),
    ]),
  ];
  $content['variable_fields'] = [
    '#type' => 'table',
    '#rows' => $rows,
    '#header' => $header,
    '#sticky' => TRUE,
  ];
  return $content;
}