You are here

public function FillEntityVariableFields::getSettingsForm in Business Rules 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/BusinessRulesAction/FillEntityVariableFields.php \Drupal\business_rules\Plugin\BusinessRulesAction\FillEntityVariableFields::getSettingsForm()

Return the form array.

@internal param array $form

Parameters

array $form: The form array.

\Drupal\Core\Form\FormStateInterface $form_state: The form state object.

\Drupal\business_rules\ItemInterface $item: The configured item.

Return value

array The render array for the settings form.

Overrides BusinessRulesItemPluginBase::getSettingsForm

File

src/Plugin/BusinessRulesAction/FillEntityVariableFields.php, line 124

Class

FillEntityVariableFields
Class FillEntityVariableFields.

Namespace

Drupal\business_rules\Plugin\BusinessRulesAction

Code

public function getSettingsForm(array &$form, FormStateInterface $form_state, ItemInterface $item) {
  $form['#attached']['library'][] = 'business_rules/style';
  $settings = [];
  if (!$item
    ->isNew()) {
    $settings['variable'] = [
      '#type' => 'select',
      '#title' => t('Entity variable'),
      '#required' => TRUE,
      '#description' => t('Entity variable to be saved. Remember to create actions to fill the entity variable fields and execute them before save the entity.'),
      '#options' => $this
        ->getAvailableEmptyVariables($item),
      '#default_value' => empty($item
        ->getSettings('variable')) ? '' : $item
        ->getSettings('variable'),
    ];
    $settings['field_value_title'] = [
      '#type' => 'item',
      '#title' => t('Fields and values for the variable'),
    ];
    $settings['field_value'] = [
      '#type' => 'table',
      '#sticky' => TRUE,
      '#header' => [
        t('Field'),
        t('Value'),
        t('Operations'),
      ],
      '#empty' => t('There are currently no values. Add one by selecting an option below.'),
    ];
    $fields_values = $item
      ->getSettings('fields_values');
    $fields = $this->util
      ->getBundleFields($item
      ->getTargetEntityType(), $item
      ->getTargetBundle());
    $settings['fields_values'] = [
      '#type' => 'value',
      '#value' => $fields_values,
    ];
    if (is_array($fields_values)) {
      foreach ($fields_values as $key => $value) {
        $settings['field_value'][$key]['#attributes'] = [
          'id' => 'field_value-' . $key,
        ];
        $settings['field_value'][$key]['entity_field'] = [
          'data' => [
            'label' => [
              '#plain_text' => $fields[$value['entity_field']],
            ],
          ],
        ];
        $settings['field_value'][$key]['field_value'] = [
          'data' => [
            'label' => [
              '#markup' => nl2br($value['field_value']),
            ],
          ],
        ];
        $links = [];
        $links['delete'] = [
          'title' => t('Remove'),
          'url' => Url::fromRoute('business_rules.plugins.action.fill_entity_variable_fields.remove_field', [
            'action' => $item
              ->id(),
            'field' => $key,
            'method' => 'nojs',
          ], [
            'attributes' => [
              'class' => [
                'use-ajax',
              ],
            ],
          ]),
        ];
        $settings['field_value'][$key]['operations'] = [
          'data' => [
            '#type' => 'operations',
            '#links' => $links,
          ],
        ];
      }
    }
    $settings['field_value']['new'] = [
      '#tree' => FALSE,
    ];
    $settings['field_value']['new']['entity_field'] = [
      'data' => [
        'entity_field' => [
          '#type' => 'select',
          '#title' => t('Field'),
          '#title_display' => 'invisible',
          '#options' => $fields,
          '#empty_option' => t('Select the field'),
        ],
      ],
      '#prefix' => '<div class="field-value-new">',
      '#suffix' => '</div>',
    ];
    $settings['field_value']['new']['field_value'] = [
      '#type' => 'textarea',
      '#rows' => 1,
      '#description' => t('The value to be set on the field. For a multi-valor field (cardinality > 1) type one value per line starting by pipeline (|) as the example:
          <br>|Value 1
          <br>|Value 2
          <br>|Value 3'),
    ];
    $settings['field_value']['new']['add'] = [
      '#type' => 'submit',
      '#value' => t('Add'),
      '#validate' => [
        get_class($this) . '::formValidate',
      ],
      '#submit' => [
        get_class($this) . '::fieldValueSave',
      ],
    ];
    $form_state
      ->set('business_rules_item', $item);
  }
  return $settings;
}