You are here

class BusinessRuleForm in Business Rules 8

Same name and namespace in other branches
  1. 2.x src/Form/BusinessRuleForm.php \Drupal\business_rules\Form\BusinessRuleForm

Class BusinessRuleForm.

@package Drupal\business_rules\Form

Hierarchy

Expanded class hierarchy of BusinessRuleForm

File

src/Form/BusinessRuleForm.php, line 23

Namespace

Drupal\business_rules\Form
View source
class BusinessRuleForm extends EntityForm {

  /**
   * The business rules Util.
   *
   * @var \Drupal\business_rules\Util\BusinessRulesUtil
   */
  public $util;

  /**
   * The reactsOnManager.
   *
   * @var \Drupal\business_rules\Plugin\BusinessRulesReactsOnManager
   */
  protected $reactsOnManager;

  /**
   * The form step.
   *
   * @var int
   */
  protected $step = 1;

  /**
   * The Business Rule flowchart.
   *
   * @var \Drupal\business_rules\Util\Flowchart\Flowchart
   */
  private $chart;

  /**
   * We don't want to use the same wait two times for an item.
   *
   * @var array
   */
  private $usedWeight = [];

  /**
   * BusinessRuleForm constructor.
   *
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   *   The services container.
   */
  public function __construct(ContainerInterface $container) {
    $this->util = $container
      ->get('business_rules.util');
    $this->entityTypeManager = $container
      ->get('entity_type.manager');
    $this->reactsOnManager = $container
      ->get('plugin.manager.business_rules.reacts_on');
    $this->chart = $container
      ->get('business_rules.flowchart');
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container);
  }

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);

    /** @var \Drupal\business_rules\Entity\BusinessRule $business_rule */
    $business_rule = $this->entity;
    if ($this->step === 1 && $business_rule
      ->isNew()) {
      $form['reacts_on'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Reacts on event'),
        '#description' => $this
          ->t('Whenever the event occurs, rule evaluation is triggered.'),
        '#options' => BusinessRule::getEventTypes(),
        '#default_value' => $business_rule
          ->getReactsOn(),
        '#required' => TRUE,
      ];
    }
    if ($this->step > 1 || !$business_rule
      ->isNew()) {
      $reactsOn = $business_rule
        ->getReactsOn() ? $business_rule
        ->getReactsOn() : $form_state
        ->getValue('reacts_on');
      $definition = $this->reactsOnManager
        ->getDefinition($reactsOn);
      $reflection = new \ReflectionClass($definition['class']);
      $custom_rule = $reflection
        ->newInstance($definition, $definition['id'], $definition);
      $form['label_reacts_on'] = [
        '#type' => 'item',
        '#title' => $this
          ->t('Reacts on event'),
        '#markup' => $definition['label'],
        '#description' => $definition['description'],
      ];
      $form['label'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Label'),
        '#maxlength' => 255,
        '#default_value' => $business_rule
          ->label(),
        '#description' => $this
          ->t("Label for the Rule."),
        '#required' => TRUE,
      ];
      $form['id'] = [
        '#type' => 'machine_name',
        '#default_value' => $business_rule
          ->id(),
        '#machine_name' => [
          'exists' => '\\Drupal\\business_rules\\Entity\\BusinessRule::load',
        ],
        '#disabled' => !$business_rule
          ->isNew(),
      ];
      $form['description'] = [
        '#type' => 'textarea',
        '#title' => $this
          ->t('Description'),
        '#description' => $this
          ->t('A good description for Business Rule.'),
        '#required' => TRUE,
        '#default_value' => $business_rule
          ->getDescription(),
      ];
      $form['tags'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Tags'),
        '#default_value' => implode(', ', $business_rule
          ->getTags()),
        '#description' => $this
          ->t('List of comma-separated tags.'),
        '#required' => FALSE,
        '#autocomplete_route_name' => 'business_rules.autocomplete_tags',
        '#autocomplete_route_parameters' => [],
      ];
      $form['status'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Enabled'),
        '#default_value' => $business_rule
          ->isNew() ? 1 : $business_rule
          ->isEnabled(),
      ];
      $form['entity'] = $this
        ->getEntityInformationForm($definition);

      // Only show items if rule is already saved.
      if (!$business_rule
        ->isNew()) {
        $form['items_container'] = [
          '#type' => 'details',
          '#description' => $this
            ->t('The items are evaluated on the presented order. Drag them to change the order.'),
          '#title' => $this
            ->t('Items'),
          '#open' => TRUE,
        ];
        $form['items_container']['items'] = $this
          ->formItems($form, $form_state);
        if (!$business_rule
          ->isNew()) {
          $form['flowchart'] = [
            '#type' => 'details',
            '#title' => $this
              ->t('Flowchart'),
            '#open' => TRUE,
          ];
          $form['flowchart']['graph'] = $this->chart
            ->getGraph($business_rule);
          $form['#attached']['library'][] = 'business_rules/mxClient';
        }
      }

      // Process the form array by the Plugin.
      $custom_rule
        ->processForm($form, $form_state);
    }
    $form['#validate'][] = '::validateForm';
    return $form;
  }

  /**
   * Get the fields for entity type and bundle.
   *
   * @param array $rule_definition
   *   The rule definition.
   *
   * @return array
   *   The render array.
   */
  public function getEntityInformationForm(array $rule_definition) {
    $form = [];

    /** @var \Drupal\business_rules\BusinessRuleInterface $rule */
    $rule = $this->entity;
    $show_entity = FALSE;
    $show_bundle = FALSE;
    if ($rule_definition['hasTargetBundle']) {
      $show_entity = TRUE;
      $show_bundle = TRUE;
    }
    elseif ($rule_definition['hasTargetEntity']) {
      $show_entity = TRUE;
    }
    if ($show_entity) {
      $form['context'] = [
        '#type' => 'fieldset',
        '#title' => $this
          ->t('Context: This information cannot be changed after the business rule 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 business rule is applicable.'),
        '#default_value' => $rule
          ->getTargetEntityType(),
        '#disabled' => $this->entity
          ->isNew() ? 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 business rule is applicable.'),
        '#options' => $this->util
          ->getBundles($rule
          ->getTargetEntityType()),
        '#required' => TRUE,
        '#default_value' => $rule
          ->getTargetBundle(),
        '#disabled' => $this->entity
          ->isNew() ? FALSE : TRUE,
        '#prefix' => '<div id="target_bundle-wrapper">',
        '#suffix' => '</div>',
      ];
    }
    return $form;
  }

  /**
   * Provide the form fields for add Business Rule's Items.
   *
   * @param array $form
   *   The form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state object.
   *
   * @return array
   *   The render array.
   */
  public function formItems(array $form, FormStateInterface $form_state) {
    $user_input = $form_state
      ->getUserInput();

    /** @var \Drupal\business_rules\Entity\BusinessRule $rule */
    $rule = $this->entity;
    $label = $this
      ->t('Item');
    $label_plural = $this
      ->t('Items');
    $items = $rule
      ->getItems();
    uasort($items, function ($a, $b) {
      return $a
        ->getWeight() < $b
        ->getWeight() ? -1 : 1;
    });
    $header = [
      'type' => $this
        ->t('Type'),
      'label' => $label,
      'weight' => $this
        ->t('Weight'),
      'id' => $this
        ->t('Machine name'),
      'subtype' => $this
        ->t('Subtype'),
      'description' => $this
        ->t('Description'),
      'operations' => $this
        ->t('Operations'),
      'business_rule_item_type' => [
        'data' => '',
        'width' => '0px',
      ],
    ];
    $table['items'] = [
      '#type' => 'table',
      '#header' => $header,
      '#attributes' => [
        'id' => 'business_rules-items',
      ],
      '#empty' => $this
        ->t('There are currently no @label in this rule. Add one by selecting an option below.', [
        '@label' => $label_plural,
      ]),
      '#tabledrag' => [
        [
          'action' => 'order',
          'relationship' => 'sibling',
          'group' => 'items-order-weight',
        ],
      ],
    ];
    if (is_array($items)) {
      foreach ($items as $value) {
        if ($value
          ->getType() == 'condition') {
          $item = Condition::load($value
            ->getId());
        }
        elseif ($value
          ->getType() == 'action') {
          $item = Action::load($value
            ->getId());
        }
        $item_weight = !empty($item) ? $value
          ->getWeight() : '';
        $route_remove_item = 'business_rules.items.remove';
        if (!empty($item)) {
          $key = $item
            ->id();
          $listBuilder = $this->entityTypeManager
            ->getListBuilder($item
            ->getEntityTypeId());
          $operations = $listBuilder
            ->buildOperations($item);
          $operations['#links']['remove'] = [
            'title' => $this
              ->t('Remove'),
            'url' => Url::fromRoute($route_remove_item, [
              'business_rule' => $rule
                ->id(),
              'item_type' => $value
                ->getType(),
              'item_id' => $item
                ->id(),
              'method' => 'nojs',
            ], [
              'attributes' => [
                'class' => [
                  'use-ajax',
                ],
              ],
            ]),
            'weight' => 1,
          ];
          uasort($operations['#links'], function ($a, $b) {
            return $a['weight'] < $b['weight'] ? -1 : 1;
          });
          foreach ($operations['#links'] as $i => $link) {
            $uri = $this->util
              ->getCurrentUri()
              ->toString();
            $operations['#links'][$i]['url']
              ->setRouteParameter('destination', $uri);
          }
          $item_weight = $this
            ->generateItemWeight('item', $item_weight);
          $weight = [
            '#type' => 'weight',
            '#title' => $this
              ->t('Weight for item'),
            '#title_display' => 'invisible',
            '#default_value' => $item_weight,
            '#delta' => 100,
            '#attributes' => [
              'class' => [
                'items-order-weight',
              ],
            ],
          ];
          $table['items'][$key] = [
            '#attributes' => [
              'class' => 'draggable',
              'id' => $item
                ->id(),
            ],
            '#weight' => isset($user_input['effects']) ? $user_input['effects'][$key]['weight'] : NULL,
            'type' => [
              '#markup' => $item
                ->getBusinessRuleItemTranslatedType(),
            ],
            'item' => [
              '#tree' => FALSE,
              'label' => [
                '#markup' => $item
                  ->label(),
              ],
            ],
            'weight' => $weight,
            'id' => [
              '#markup' => $item
                ->id(),
            ],
            'subtype' => [
              '#markup' => $item
                ->getTypeLabel(),
            ],
            'description' => [
              '#markup' => $item
                ->getDescription(),
            ],
            'operations' => $operations,
            'business_rule_item_type' => [
              '#type' => 'value',
              '#value' => $value
                ->getType(),
            ],
          ];
        }
      }
    }
    $add_condition = Link::createFromRoute($this
      ->t('Add Condition'), 'business_rules.items.table', [
      'business_rule' => $rule
        ->id(),
      'item_type' => 'condition',
      'method' => 'nojs',
    ], [
      'attributes' => [
        'class' => [
          'use-ajax',
        ],
      ],
    ]);
    $add_action = Link::createFromRoute($this
      ->t('Add Action'), 'business_rules.items.table', [
      'business_rule' => $rule
        ->id(),
      'item_type' => 'action',
      'method' => 'nojs',
    ], [
      'attributes' => [
        'class' => [
          'use-ajax',
        ],
      ],
    ]);
    $table['add'][] = [
      'data' => [
        'add' => [
          '#type' => 'markup',
          '#markup' => $add_condition
            ->toString() . ' | ' . $add_action
            ->toString(),
          '#prefix' => '<div id="business_rule-add_buttons">',
          '#suffix' => '</div>',
        ],
      ],
    ];
    return $table;
  }

  /**
   * Generate the item weight.
   *
   * @param string $settings_type
   *   The settings type.
   * @param int $weight
   *   The item weight.
   *
   * @return int
   *   The generated weight.
   */
  private function generateItemWeight($settings_type, $weight) {
    if (!isset($this->usedWeight[$settings_type])) {
      $this->usedWeight[$settings_type][] = $weight;
      return $weight;
    }
    if (!in_array($weight, $this->usedWeight[$settings_type])) {
      $this->usedWeight[$settings_type][] = $weight;
      return $weight;
    }
    else {
      $weight++;
      return $this
        ->generateItemWeight($settings_type, $weight);
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function actionsElement(array $form, FormStateInterface $form_state) {
    $actions = parent::actionsElement($form, $form_state);
    if (!$this->entity
      ->isNew()) {
      $actions['done'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Done'),
        '#submit' => [
          '::submitForm',
          '::save',
        ],
        '#op' => 'done',
        '#weight' => 7,
        '#validate' => [
          '::validateForm',
        ],
      ];
    }
    elseif ($this->step === 1) {
      $actions['submit']['#value'] = $this
        ->t('Continue');
    }
    $actions['submit']['#op'] = 'save';
    return $actions;
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    if ($this->step < 2 && $this->entity
      ->isNew()) {
      $this->step++;
      $form_state
        ->setRebuild();
      return $form;
    }
    else {

      /** @var \Drupal\business_rules\Entity\BusinessRule $business_rule */
      $business_rule = $this->entity;
      $items = $form_state
        ->getValue('items');
      $business_rule
        ->set('items', []);
      $br_items = [];
      if (is_array($items)) {
        foreach ($items as $key => $value) {
          $itemObj = new BusinessRulesItemObject($key, $value['business_rule_item_type'], $value['weight']);
          $br_items[$value['weight']] = $itemObj;
        }
      }
      ksort($br_items);
      foreach ($br_items as $item) {
        $business_rule
          ->addItem($item);
      }
      $business_rule
        ->setTags(explode(',', $form_state
        ->getValue('tags')));
      $status = $business_rule
        ->save();

      // As the rule may need to be executed under a cached hook, we need to
      // invalidate all rendered caches.
      Cache::invalidateTags([
        'rendered',
      ]);
      switch ($status) {
        case SAVED_NEW:
          drupal_set_message($this
            ->t('Created the %label Rule.', [
            '%label' => $business_rule
              ->label(),
          ]));
          break;
        default:
          drupal_set_message($this
            ->t('Saved the %label Rule.', [
            '%label' => $business_rule
              ->label(),
          ]));
      }
      if (isset($form_state
        ->getTriggeringElement()['#op'])) {
        $op = $form_state
          ->getTriggeringElement()['#op'];
        if ($op == 'save') {
          $form_state
            ->setRedirectUrl($business_rule
            ->urlInfo('edit-form', [
            'business_rule' => $business_rule
              ->id(),
          ]));
        }
        else {
          $form_state
            ->setRedirectUrl($business_rule
            ->urlInfo('collection'));
        }
      }
    }
    return $status;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {

    // Validate de Business Rule Machine Name.
    $id = $form_state
      ->getValue('id');
    if ($id && $this->entity
      ->isNew()) {
      $br = BusinessRule::load($id);
      if (!empty($br)) {
        $form_state
          ->setErrorByName('id', $this
          ->t('The machine-readable name is already in use. It must be unique.'));
      }
    }
  }

  /**
   * Populates target_bundle options according to the selected entity type.
   *
   * @param array $form
   *   The form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state object.
   *
   * @return \Drupal\Core\Ajax\AjaxResponse
   *   The AjaxResponse.
   */
  public function targetEntityTypeCallback(array &$form, FormStateInterface $form_state) {
    $target_entity_type = $form_state
      ->getValue('target_entity_type');
    if (!empty($target_entity_type)) {
      $form['entity']['context']['target_bundle']['#options'] = $this->util
        ->getBundles($target_entity_type);
    }
    $response = new AjaxResponse();
    $response
      ->addCommand(new ReplaceCommand('#target_bundle-wrapper', $form['entity']['context']['target_bundle']));
    return $response;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BusinessRuleForm::$chart private property The Business Rule flowchart.
BusinessRuleForm::$reactsOnManager protected property The reactsOnManager.
BusinessRuleForm::$step protected property The form step.
BusinessRuleForm::$usedWeight private property We don't want to use the same wait two times for an item.
BusinessRuleForm::$util public property The business rules Util.
BusinessRuleForm::actionsElement protected function Returns the action form element for the current entity form. Overrides EntityForm::actionsElement
BusinessRuleForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
BusinessRuleForm::form public function Gets the actual form array to be built. Overrides EntityForm::form
BusinessRuleForm::formItems public function Provide the form fields for add Business Rule's Items.
BusinessRuleForm::generateItemWeight private function Generate the item weight.
BusinessRuleForm::getEntityInformationForm public function Get the fields for entity type and bundle.
BusinessRuleForm::save public function Form submission handler for the 'save' action. Overrides EntityForm::save
BusinessRuleForm::targetEntityTypeCallback public function Populates target_bundle options according to the selected entity type.
BusinessRuleForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
BusinessRuleForm::__construct public function BusinessRuleForm constructor.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EntityForm::$entity protected property The entity being used by this form. 7
EntityForm::$entityTypeManager protected property The entity type manager. 3
EntityForm::$moduleHandler protected property The module handler service.
EntityForm::$operation protected property The name of the current operation.
EntityForm::$privateEntityManager private property The entity manager.
EntityForm::actions protected function Returns an array of supported actions for the current entity form. 29
EntityForm::afterBuild public function Form element #after_build callback: Updates the entity with submitted data.
EntityForm::buildEntity public function Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface::buildEntity 2
EntityForm::buildForm public function Form constructor. Overrides FormInterface::buildForm 10
EntityForm::copyFormValuesToEntity protected function Copies top-level form values to entity properties 7
EntityForm::getBaseFormId public function Returns a string identifying the base form. Overrides BaseFormIdInterface::getBaseFormId 5
EntityForm::getEntity public function Gets the form entity. Overrides EntityFormInterface::getEntity
EntityForm::getEntityFromRouteMatch public function Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface::getEntityFromRouteMatch 1
EntityForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId 10
EntityForm::getOperation public function Gets the operation identifying the form. Overrides EntityFormInterface::getOperation
EntityForm::init protected function Initialize the form state and the entity before the first form build. 3
EntityForm::prepareEntity protected function Prepares the entity object before the form is built first. 3
EntityForm::prepareInvokeAll protected function Invokes the specified prepare hook variant.
EntityForm::processForm public function Process callback: assigns weights and hides extra fields.
EntityForm::setEntity public function Sets the form entity. Overrides EntityFormInterface::setEntity
EntityForm::setEntityManager public function Sets the entity manager for this form. Overrides EntityFormInterface::setEntityManager
EntityForm::setEntityTypeManager public function Sets the entity type manager for this form. Overrides EntityFormInterface::setEntityTypeManager
EntityForm::setModuleHandler public function Sets the module handler for this form. Overrides EntityFormInterface::setModuleHandler
EntityForm::setOperation public function Sets the operation for this form. Overrides EntityFormInterface::setOperation
EntityForm::submitForm public function This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state… Overrides FormInterface::submitForm 17
EntityForm::__get public function
EntityForm::__set public function
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.