You are here

public function UrlVariable::changeDetails in Business Rules 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/BusinessRulesVariable/UrlVariable.php \Drupal\business_rules\Plugin\BusinessRulesVariable\UrlVariable::changeDetails()

Change the variable details box.

Give a chance to each variable plugin to change the variable details row on Available Variables Box.

Parameters

\Drupal\business_rules\Entity\Variable $variable: The variable.

array $row: The row which contains the variable.

Overrides BusinessRulesVariablePlugin::changeDetails

File

src/Plugin/BusinessRulesVariable/UrlVariable.php, line 46

Class

UrlVariable
A variable representing the current url.

Namespace

Drupal\business_rules\Plugin\BusinessRulesVariable

Code

public function changeDetails(Variable $variable, array &$row) {

  // Show a link to a modal window which variable description.
  $header = [
    'variable' => t('Variable'),
    'field' => t('Field'),
    'type' => t('Type'),
  ];
  $url = $_SERVER['REQUEST_URI'];
  $fields = explode('/', $url);
  unset($fields[0]);
  $rows = [];
  $rows[] = [
    'variable' => [
      'data' => [
        '#markup' => '{{' . $variable
          ->id() . '}}',
      ],
    ],
    'field' => [
      'data' => [
        '#markup' => $url,
      ],
    ],
    'type' => [
      'data' => [
        '#markup' => t('String'),
      ],
    ],
  ];
  foreach ($fields as $key => $value) {
    $rows[] = [
      'variable' => [
        'data' => [
          '#markup' => '{{' . $variable
            ->id() . '->' . $key . '}}',
        ],
      ],
      'field' => [
        'data' => [
          '#markup' => $value,
        ],
      ],
      'type' => [
        'data' => [
          '#markup' => t('String'),
        ],
      ],
    ];
  }
  $content['description'] = [
    '#type' => 'markup',
    '#markup' => t('As an example, the current Url would return the following values:'),
  ];
  $content['variable_fields'] = [
    '#type' => 'table',
    '#rows' => $rows,
    '#header' => $header,
    '#sticky' => TRUE,
  ];
  $keyvalue = $this->util
    ->getKeyValueExpirable('url_variable');
  $keyvalue
    ->set('url_variable.' . $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' => 'url_variable',
    'key' => 'url_variable.' . $variable
      ->id(),
  ], [
    'attributes' => [
      'class' => [
        'use-ajax',
      ],
    ],
  ])
    ->toString();
  $row['description']['data']['#markup'] .= '<br>' . $details_link;
}