You are here

public function ItemForm::getEntityInformationForm in Business Rules 2.x

Same name and namespace in other branches
  1. 8 src/Form/ItemForm.php \Drupal\business_rules\Form\ItemForm::getEntityInformationForm()

Get the fields for entity type and bundle.

Parameters

array $item_definition: The item definition.

Return value

array The render array.

1 call to ItemForm::getEntityInformationForm()
ItemForm::form in src/Form/ItemForm.php
Gets the actual form array to be built.

File

src/Form/ItemForm.php, line 234

Class

ItemForm
Base class for Business rules item.

Namespace

Drupal\business_rules\Form

Code

public function getEntityInformationForm(array $item_definition) {
  $form = [];

  /** @var \Drupal\business_rules\ItemInterface $item */
  $item = $this->entity;
  $show_entity = FALSE;
  $show_bundle = FALSE;
  $show_field = FALSE;
  $context_dependent = $item_definition['isContextDependent'];
  if ($item_definition['hasTargetField']) {
    $show_entity = TRUE;
    $show_bundle = TRUE;
    $show_field = TRUE;
  }
  elseif ($item_definition['hasTargetBundle']) {
    $show_entity = TRUE;
    $show_bundle = TRUE;
  }
  elseif ($item_definition['hasTargetEntity']) {
    $show_entity = TRUE;
  }
  if ($show_entity) {
    if ($context_dependent) {
      $form['context'] = [
        '#type' => 'fieldset',
        '#title' => $this
          ->t('Context: This information cannot be changed after the item is saved.'),
      ];
    }
    $form['context']['target_entity_type'] = [
      '#type' => 'select',
      '#options' => $this->util
        ->getEntityTypes(),
      '#required' => TRUE,
      '#title' => $this
        ->t('Target Entity Type'),
      '#description' => $this
        ->t('The Entity Type which this item is applicable.'),
      '#default_value' => $item
        ->getTargetEntityType(),
      '#disabled' => $this->entity
        ->isNew() || !$context_dependent ? FALSE : TRUE,
    ];
  }
  if ($show_bundle) {
    $form['context']['target_entity_type']['#ajax'] = [
      'callback' => [
        $this,
        'targetEntityTypeCallback',
      ],
    ];
    $form['context']['target_bundle'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Target Bundle'),
      '#description' => $this
        ->t('The Bundle which this item is applicable.'),
      '#options' => $this->util
        ->getBundles($item
        ->getTargetEntityType()),
      '#required' => TRUE,
      '#default_value' => $item
        ->getTargetBundle(),
      '#disabled' => $this->entity
        ->isNew() || !$context_dependent ? FALSE : TRUE,
      '#prefix' => '<div id="target_bundle-wrapper">',
      '#suffix' => '</div>',
    ];
  }
  if ($show_field) {
    $form['context']['target_bundle']['#ajax'] = [
      'callback' => [
        $this,
        'targetBundleCallback',
      ],
    ];
    $form['field'] = [
      '#type' => 'select',
      '#options' => $this->util
        ->getBundleFields($item
        ->getTargetEntityType(), $item
        ->getTargetBundle()),
      '#required' => TRUE,
      '#disabled' => FALSE,
      '#title' => $this
        ->t('Field'),
      '#description' => $this
        ->t('The entity field.'),
      '#default_value' => $item
        ->getSettings('field'),
      '#prefix' => '<div id="field_selector-wrapper">',
      '#suffix' => '</div>',
    ];
  }
  return $form;
}