ifthenelse-8.x-1.x-dev-no-core/modules/contrib/if_then_else/src/core/Nodes/Actions/AddFormFieldAction/AddFormFieldAction.php

modules/contrib/if_then_else/src/core/Nodes/Actions/AddFormFieldAction/AddFormFieldAction.php
<?php

namespace Drupal\if_then_else\core\Nodes\Actions\AddFormFieldAction;

use Drupal\if_then_else\core\Nodes\Actions\Action;
use Drupal\if_then_else\Event\GraphValidationEvent;
use Drupal\if_then_else\Event\NodeSubscriptionEvent;

/**
 * Add form field action class.
 */
class AddFormFieldAction extends Action {

  /**
   * Return name of node.
   */
  public static function getName() {
    return 'add_form_field_action';
  }

  /**
   * {@inheritdoc}
   */
  public function registerNode(NodeSubscriptionEvent $event) {
    $event->nodes[static::getName()] = [
      'label' => t('Add Form Field'),
      'type' => 'action',
      'class' => 'Drupal\\if_then_else\\core\\Nodes\\Actions\\AddFormFieldAction\\AddFormFieldAction',
      'inputs' => [
        'form' => [
          'label' => t('Form'),
          'description' => t('Form object.'),
          'sockets' => ['form'],
          'required' => TRUE,
        ],
        'name' => [
          'label' => t('Name'),
          'description' => t('Set name of form element.'),
          'sockets' => ['string'],
          'required' => TRUE,
        ],
        'value' => [
          'label' => t('Value'),
          'description' => t('Add value to form element.'),
          'sockets' => ['string'],
          'required' => TRUE,
        ],
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function validateGraph(GraphValidationEvent $event) {
    $nodes = $event->data->nodes;
    foreach ($nodes as $node) {
      if ($node->data->type == 'value' && $node->data->name == 'text_value') {
        // To check empty input.
        if (!property_exists($node->data, 'value') || empty($node->data->value)) {
          $event->errors[] = t('Enter the name of field in  "@node_name".', ['@node_name' => $node->name]);
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function process() {

    $form = &$this->inputs['form'];
    $name = &$this->inputs['name'];
    $value = &$this->inputs['value'];

    $machine_readable = strtolower($name);
    $machine_readable = preg_replace('@[^a-z0-9_]+@', '_', $machine_readable);

    $form[$machine_readable] = [
      '#type' => 'hidden',
      '#title' => t('@name', ['@name' => $name]),
      '#value' => $value,
    ];
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc