You are here

public function MaestroTemplateFormBase::buildForm in Maestro 8.2

Same name and namespace in other branches
  1. 3.x src/Form/MaestroTemplateFormBase.php \Drupal\maestro\Form\MaestroTemplateFormBase::buildForm()

Overrides Drupal\Core\Entity\EntityFormController::form().

Builds the entity add/edit form.

Parameters

array $form: An associative array containing the structure of the form.

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

Return value

array An associative array containing the Template add/edit form.

Overrides EntityForm::buildForm

2 calls to MaestroTemplateFormBase::buildForm()
MaestroTemplateAddForm::buildForm in src/Form/MaestroTemplateAddForm.php
Overrides Drupal\Core\Entity\EntityFormController::form().
MaestroTemplateEditForm::buildForm in src/Form/MaestroTemplateEditForm.php
Overrides Drupal\Core\Entity\EntityFormController::form().
2 methods override MaestroTemplateFormBase::buildForm()
MaestroTemplateAddForm::buildForm in src/Form/MaestroTemplateAddForm.php
Overrides Drupal\Core\Entity\EntityFormController::form().
MaestroTemplateEditForm::buildForm in src/Form/MaestroTemplateEditForm.php
Overrides Drupal\Core\Entity\EntityFormController::form().

File

src/Form/MaestroTemplateFormBase.php, line 34

Class

MaestroTemplateFormBase
Class MaestroTemplateFormBase.

Namespace

Drupal\maestro\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // Get anything we need form the base class.
  $form = parent::buildForm($form, $form_state);
  $isModal = $this
    ->getRequest()
    ->get('is_modal');
  $Template = $this->entity;

  // If we're modal, we remove the delete action.
  if ($isModal == 'modal') {
    unset($form['actions']['delete']);
  }

  // Build the form.
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $Template
      ->label(),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#title' => $this
      ->t('Machine name'),
    '#default_value' => $Template
      ->id(),
    '#machine_name' => [
      'exists' => [
        get_class($this),
        'exists',
      ],
    ],
  ];

  // The notion of App Groups will be carried across to D8 Maestro, however we
  // will simply hide for the time being as they were used sparingly in the Drupal 7 version.
  $form['app_group'] = [
    '#type' => 'hidden',
    '#title' => $this
      ->t('App Group'),
    '#maxlength' => 255,
    '#default_value' => isset($Template->app_group) ? $Template->app_group : 0,
    '#required' => TRUE,
  ];

  // Canvas height is a default value of 900 pixels.
  $form['canvas_height'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Canvas Height in pixels'),
    '#maxlength' => 255,
    '#default_value' => isset($Template->canvas_height) ? $Template->canvas_height : 900,
    '#required' => TRUE,
  ];

  // Default is 800 px wide.
  $form['canvas_width'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Canvas Width in pixels'),
    '#maxlength' => 255,
    '#default_value' => isset($Template->canvas_width) ? $Template->canvas_width : 800,
    '#required' => TRUE,
  ];

  // Default is to not show details.
  $form['show_details'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t("Show details of this template's process in Task Console details?"),
    '#description' => $this
      ->t('When checked, the task console will enable the showing of details of the process/task.'),
    '#default_value' => isset($Template->show_details) ? $Template->show_details : 0,
  ];
  $form['show_details_area'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Details Configuration'),
    '#states' => [
      'visible' => [
        ':input[name="show_details"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $all_views = Views::getAllViews();
  $options = [
    '' => $this
      ->t('Select View'),
  ];
  foreach ($all_views as $machine_name => $view) {
    $options[$machine_name] = $view
      ->label();
  }
  $form['show_details_area']['add_view'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Add a view to the details panel.'),
    '#description' => $this
      ->t('You can add a view to be displayed in the details panel in the task console. QueueID and ProcessID are passed to the view as arguments.'),
    '#default_value' => '',
    '#options' => $options,
    '#required' => FALSE,
    '#states' => [
      'visible' => [
        ':input[name="show_details"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
    '#ajax' => [
      'callback' => '::fetchViewsDisplays',
      'wrapper' => 'dropdown-views-bundles-replace',
    ],
  ];

  // Need to do an auto-lookup to get the view's display only cone the add_view has changed.
  $form['show_details_area']['add_view_display'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Select the view display.'),
    '#description' => $this
      ->t('The display of the view you wish to add.'),
    '#default_value' => '',
    '#options' => [],
    '#required' => FALSE,
    '#states' => [
      'visible' => [
        ':input[name="show_details"]' => [
          'checked' => TRUE,
        ],
        ':input[name="add_view"]' => [
          '!value' => '',
        ],
      ],
    ],
    '#validated' => TRUE,
    '#prefix' => '<div id="dropdown-views-bundles-replace">',
    '#suffix' => '</div>',
  ];
  $form['show_details_area']['views'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Views attached to details output'),
    '#prefix' => '<div id="views-replace">',
    '#suffix' => '</div>',
    '#open' => TRUE,
    '#states' => [
      'visible' => [
        ':input[name="show_details"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];

  // Generate the list of views attached with the option to delete only.
  $views_array = [];
  if (isset($this->entity->views_attached)) {
    foreach ($this->entity->views_attached as $key => $view_information) {
      $display = explode(';', $view_information['view_display']);
      $views_array[$view_information['view_weight']] = [
        'machine_name' => $view_information['view_machine_name'],
        'label' => Views::getView($view_information['view_machine_name'])->storage
          ->label(),
        'view_display' => isset($display[1]) ? $display[1] : '',
      ];
    }
  }

  // Now have a sorted by weight list of views.  Generate the table.
  $form['show_details_area']['views']['views_attached_to_template'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Name'),
      $this
        ->t('Machine name'),
      $this
        ->t('Display'),
      $this
        ->t('Weight'),
      $this
        ->t('Remove'),
    ],
    '#empty' => $this
      ->t('There are no attached views'),
    '#tableselect' => FALSE,
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'views-attached-to-template-order-weight',
      ],
    ],
  ];
  foreach ($views_array as $weight => $view) {
    $form['show_details_area']['views']['views_attached_to_template'][$weight]['#attributes']['class'][] = 'draggable';
    $form['show_details_area']['views']['views_attached_to_template'][$weight]['#weight'] = $weight;
    $form['show_details_area']['views']['views_attached_to_template'][$weight]['name'] = [
      '#plain_text' => $view['label'],
    ];
    $form['show_details_area']['views']['views_attached_to_template'][$weight]['machine_name'] = [
      '#plain_text' => $view['machine_name'],
    ];
    $form['show_details_area']['views']['views_attached_to_template'][$weight]['display'] = [
      '#plain_text' => $view['view_display'],
    ];
    $form['show_details_area']['views']['views_attached_to_template'][$weight]['weight'] = [
      '#type' => 'weight',
      '#title' => $this
        ->t('Order'),
      '#title_display' => 'invisible',
      '#default_value' => $weight,
      // Classify the weight element for #tabledrag.
      '#attributes' => [
        'class' => [
          'views-attached-to-template-order-weight',
        ],
      ],
    ];
    $form['show_details_area']['views']['views_attached_to_template'][$weight]['delete'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Delete'),
      '#title_display' => 'none',
      '#required' => FALSE,
    ];
  }

  // Default is 0.
  $form['default_workflow_timeline_stage_count'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('The default number of stages you wish to show this workflow having'),
    '#description' => $this
      ->t('Default 0 will show no stages in the task console. Process variable for worflow_timeline_stage_count will
          be set with the value configured'),
    '#maxlength' => 3,
    '#size' => 3,
    '#default_value' => isset($Template->default_workflow_timeline_stage_count) ? $Template->default_workflow_timeline_stage_count : 0,
    '#required' => TRUE,
  ];

  // Variable definitions:
  // Complex type of machine name and variable value.
  $form['variables'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Variables'),
    '#group' => 'templatevariables',
    '#tree' => TRUE,
    '#prefix' => '<div id="multi-replace">',
    '#suffix' => '</div>',
  ];
  $key = 0;

  // See /src/Form/MaestroTemplateAddForm.php.
  $mandatory_variables = [
    'initiator',
    'workflow_timeline_stage_count',
    'workflow_current_stage',
    'workflow_current_stage_message',
  ];
  if (isset($this->entity->variables)) {
    foreach ($this->entity->variables as $key => $variable) {
      $disabled = FALSE;
      $attributes = NULL;
      $class = 'maestro-existing-variable';
      if (array_search($key, $mandatory_variables) !== FALSE) {

        // Do not allow the mandatory variables to be deleted.
        $disabled = TRUE;
        $class = 'maestro-existing-variable-no-delete';
        $attributes = [
          'title' => t("You cannot delete this variable.  This variable is required by Maestro."),
        ];
      }
      $form['variables'][$key]['delete'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Delete'),
        '#title_display' => 'before',
        '#required' => FALSE,
        '#prefix' => '<div class="clearfix ' . $class . '">',
        '#disabled' => $disabled,
        '#attributes' => $attributes,
      ];
      $form['variables'][$key]['variable_id'] = [
        '#type' => 'machine_name',
        '#title' => $this
          ->t('Variable name'),
        '#default_value' => isset($this->entity->variables[$key]['variable_id']) ? $this->entity->variables[$key]['variable_id'] : '',
        '#machine_name' => [
          'exists' => [
            get_class($this),
            'exists',
          ],
        ],
        '#required' => FALSE,
        '#disabled' => $disabled,
      ];
      $form['variables'][$key]['variable_value'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Value'),
        '#maxlength' => 255,
        '#default_value' => isset($this->entity->variables[$key]['variable_value']) ? $this->entity->variables[$key]['variable_value'] : '',
        '#required' => FALSE,
        '#suffix' => '</div>',
      ];
    }
  }

  // Form elements for a new variable.
  $form['variables']['new_fieldset'] = [
    '#type' => 'fieldset',
    '#title' => 'Create New Variable',
  ];
  $form['variables']['new_fieldset']['new']['variable_id'] = [
    '#type' => 'machine_name',
    '#title' => $this
      ->t('Variable name'),
    '#default_value' => '',
    '#machine_name' => [
      'exists' => [
        get_class($this),
        'exists',
      ],
    ],
    '#required' => FALSE,
  ];
  $form['variables']['new_fieldset']['new']['variable_value'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Value'),
    '#maxlength' => 255,
    '#default_value' => '',
    '#required' => FALSE,
  ];
  $form['#attached']['library'][] = 'maestro/maestro-engine-css';
  return $form;
}