You are here

public function WebformOptionsCustomForm::form in Webform 8.5

Same name and namespace in other branches
  1. 6.x modules/webform_options_custom/src/WebformOptionsCustomForm.php \Drupal\webform_options_custom\WebformOptionsCustomForm::form()

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

modules/webform_options_custom/src/WebformOptionsCustomForm.php, line 82

Class

WebformOptionsCustomForm
Provides a form to set webform options custom.

Namespace

Drupal\webform_options_custom

Code

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

  /** @var \Drupal\webform_options_custom\WebformOptionsCustomInterface $webform_options_custom */
  $webform_options_custom = $this->entity;
  switch ($this->operation) {
    case 'preview':

      // Build and return a preview of the custom options element.
      $form['preview'] = $webform_options_custom
        ->getPreview();
      return $form;
    case 'source':

      // Build and return a options YAML source element.
      $form['options'] = [
        '#type' => 'details',
        '#title' => $this
          ->t('Options'),
        '#open' => TRUE,
      ];
      $form['options']['options_message'] = [
        '#type' => 'webform_message',
        '#message_type' => 'info',
        '#message_message' => $this
          ->t('Below options are used to enhance and also translate the custom options parsed from the HTML/SVG markup.'),
        '#message_close' => TRUE,
        '#message_storage' => WebformMessage::STORAGE_SESSION,
      ];
      $form['options']['options'] = [
        '#type' => 'webform_codemirror',
        '#mode' => 'yaml',
        '#title' => $this
          ->t('Options (YAML)'),
        '#title_displace' => 'invisible',
        '#attributes' => [
          'style' => 'min-height: 200px',
        ],
        '#default_value' => $this
          ->getOptions(),
      ];
      return parent::form($form, $form_state);
  }

  /** @var \Drupal\webform_options_custom\WebformOptionsCustomStorageInterface $webform_options_custom_storage */
  $webform_options_custom_storage = $this->entityTypeManager
    ->getStorage('webform_options_custom');

  // General.
  $form['general'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('General'),
    '#open' => TRUE,
  ];
  $form['general']['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#required' => TRUE,
    '#attributes' => $webform_options_custom
      ->isNew() ? [
      'autofocus' => 'autofocus',
    ] : [],
    '#default_value' => $webform_options_custom
      ->label(),
  ];
  $form['general']['id'] = [
    '#type' => 'machine_name',
    '#machine_name' => [
      'exists' => '\\Drupal\\webform_options_custom\\Entity\\WebformOptionsCustom::load',
      'label' => '<br/>' . $this
        ->t('Machine name'),
      'source' => [
        'general',
        'label',
      ],
    ],
    '#maxlength' => 32,
    '#field_suffix' => $webform_options_custom
      ->isNew() ? ' (' . $this
      ->t('Maximum @max characters', [
      '@max' => 32,
    ]) . ')' : '',
    '#required' => TRUE,
    '#disabled' => !$webform_options_custom
      ->isNew(),
    '#default_value' => $webform_options_custom
      ->id(),
  ];
  $form['general']['description'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Description'),
    '#description' => $this
      ->t('A brief description present to the user when adding this element to a webform.'),
    '#required' => TRUE,
    '#rows' => 2,
    '#default_value' => $webform_options_custom
      ->get('description'),
  ];
  $form['general']['help'] = [
    '#type' => 'webform_html_editor',
    '#title' => $this
      ->t('Help text'),
    '#description' => $this
      ->t('Instructions to present to the user below this element on the editing form.'),
    '#default_value' => $webform_options_custom
      ->get('help'),
  ];
  $form['general']['category'] = [
    '#type' => 'webform_select_other',
    '#title' => $this
      ->t('Category'),
    '#options' => $webform_options_custom_storage
      ->getCategories(),
    '#empty_option' => $this
      ->t('- None -'),
    '#default_value' => $webform_options_custom
      ->get('category'),
  ];

  // Template.
  $form['template'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Template'),
    '#open' => TRUE,
  ];
  $form['template']['type'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Type'),
    '#options' => [
      WebformOptionsCustomInterface::TYPE_URL => $this
        ->t('URL'),
      WebformOptionsCustomInterface::TYPE_TEMPLATE => $this
        ->t('Template'),
    ],
    '#options_display' => 'side_by_side',
    '#default_value' => $webform_options_custom
      ->get('type'),
    '#required' => TRUE,
  ];
  $form['template']['url'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('HTML/SVG file URL or path'),
    '#description' => $this
      ->t('Enter the absolute URL or root-relative path to the HTML/SVG file. The HTML/SVG file must be publicly accessible using http:// or https://.'),
    '#default_value' => $webform_options_custom
      ->get('url'),
    '#states' => [
      'visible' => [
        ':input[name="type"]' => [
          'value' => WebformOptionsCustomInterface::TYPE_URL,
        ],
      ],
      'required' => [
        ':input[name="type"]' => [
          'value' => WebformOptionsCustomInterface::TYPE_URL,
        ],
      ],
    ],
  ];
  if (function_exists('imce_process_url_element')) {
    imce_process_url_element($form['template']['url'], 'link');
    $form['#attached']['library'][] = 'webform/imce.input';
  }
  $form['template']['template'] = [
    '#type' => 'webform_codemirror',
    '#mode' => 'twig',
    '#title' => $this
      ->t('HTML/SVG markup template (Twig)'),
    '#description' => $this
      ->t('The entire element with descriptions without the hash (#) prefixes is passed as variables to the Twig template.'),
    '#default_value' => $webform_options_custom
      ->get('template'),
    '#states' => [
      'visible' => [
        ':input[name="type"]' => [
          'value' => WebformOptionsCustomInterface::TYPE_TEMPLATE,
        ],
      ],
      'required' => [
        ':input[name="type"]' => [
          'value' => WebformOptionsCustomInterface::TYPE_TEMPLATE,
        ],
      ],
    ],
  ];
  $form['template']['twig_help'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Help using Twig'),
    'description' => $this
      ->buildTwigHelp(),
    '#states' => [
      'visible' => [
        ':input[name="type"]' => [
          'value' => WebformOptionsCustomInterface::TYPE_TEMPLATE,
        ],
      ],
      'required' => [
        ':input[name="type"]' => [
          'value' => WebformOptionsCustomInterface::TYPE_TEMPLATE,
        ],
      ],
    ],
  ];
  $form['template']['value_attributes'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Option value attribute names'),
    '#description' => $this
      ->t('Enter a comma-delimited list of attribute names. The first matched value attribute will be used for all custom option values .Leave blank if options values is populated using Twig.'),
    '#pattern' => '^([-_,]|[a-z])+$',
    '#default_value' => $webform_options_custom
      ->get('value_attributes'),
  ];
  $form['template']['text_attributes'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Option text attribute names'),
    '#description' => $this
      ->t('Enter a comma-delimited list of attribute names. The first matched text attribute will be used for all custom option text. Leave blank if options text is populated using Twig.'),
    '#pattern' => '^([-_,]|[a-z])+$',
    '#default_value' => $webform_options_custom
      ->get('text_attributes'),
  ];
  $form['template']['fill'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Allow SVG option elements to be filled using CSS'),
    '#description' => $this
      ->t('If checked, inline fill styles will be removed and replaced using CSS.'),
    '#return_value' => TRUE,
    '#default_value' => $webform_options_custom
      ->get('fill'),
  ];
  $form['template']['zoom'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable SVG panning and zooming'),
    '#description' => $this
      ->t('If checked, SVG graphic can be panned and zooming using the <a href=":href">svg-pan-zoom</a> library', [
      ':href' => 'https://github.com/ariutta/svg-pan-zoom',
    ]),
    '#return_value' => TRUE,
    '#default_value' => $webform_options_custom
      ->get('zoom'),
  ];
  $form['assets'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('CSS/JS'),
    '#open' => TRUE,
  ];
  $form['assets']['css'] = [
    '#type' => 'webform_codemirror',
    '#mode' => 'css',
    '#title' => $this
      ->t('Custom CSS'),
    '#default_value' => $webform_options_custom
      ->get('css'),
  ];
  $form['assets']['javascript'] = [
    '#type' => 'webform_codemirror',
    '#mode' => 'javascript',
    '#title' => $this
      ->t('Custom JavaScript'),
    '#default_value' => $webform_options_custom
      ->get('javascript'),
  ];

  // Options.
  $form['options'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Options'),
    '#open' => TRUE,
  ];
  $form['options']['options_message'] = [
    '#type' => 'webform_message',
    '#message_type' => 'info',
    '#message_message' => $this
      ->t('Below options are used to enhance and also translate the custom options parsed from the HTML/SVG markup.'),
    '#message_close' => TRUE,
    '#message_storage' => WebformMessage::STORAGE_SESSION,
  ];
  $form['options']['options'] = [
    '#type' => 'webform_options',
    '#title' => $this
      ->t('Options'),
    '#descriptions' => $this
      ->t('Option descriptions are displayed by tooltips.'),
    '#title_displace' => 'invisible',
    '#options_description' => TRUE,
    '#empty_options' => 10,
    '#add_more_items' => 10,
    '#default_value' => $this
      ->getOptions(),
  ];
  $form['options']['show_select'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Show select menu associated with the custom options element'),
    '#description' => $this
      ->t('If checked, the select menu associated with the custom options element will be visible. Displaying the standard HTML select menu assists mobile users and users with disabilities when thy are selecting custom options.'),
    '#return_value' => TRUE,
    '#default_value' => $webform_options_custom
      ->get('show_select'),
  ];
  $form['options']['tooltip'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Display option text and description in a tooltip'),
    '#description' => $this
      ->t('If checked, option text and description will be displayed using a tooltip.'),
    '#return_value' => TRUE,
    '#default_value' => $webform_options_custom
      ->get('tooltip'),
  ];

  // Integration.
  $form['integration'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Integration'),
    '#open' => TRUE,
  ];
  $form['integration']['element'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Use as a basic select element'),
    '#description' => $this
      ->t('If checked, this custom options element will be available when a user is adding elements to a webform.'),
    '#return_value' => TRUE,
    '#default_value' => $webform_options_custom
      ->get('element'),
  ];
  $form['integration']['entity_reference'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Use as an entity reference element'),
    '#description' => $this
      ->t('If checked, this custom options element will be available for entity references when a user is adding elements to a webform.'),
    '#return_value' => TRUE,
    '#default_value' => $webform_options_custom
      ->get('entity_reference'),
  ];
  return parent::form($form, $form_state);
}