You are here

public function WebformContentCreatorManageFieldsForm::constructTable in Webform Content Creator 2.x

Same name and namespace in other branches
  1. 8 src/Form/WebformContentCreatorManageFieldsForm.php \Drupal\webform_content_creator\Form\WebformContentCreatorManageFieldsForm::constructTable()
  2. 3.x src/Form/WebformContentCreatorManageFieldsForm.php \Drupal\webform_content_creator\Form\WebformContentCreatorManageFieldsForm::constructTable()

Constructs table with mapping between webform and bundle.

Parameters

array $form: Form entity array.

1 call to WebformContentCreatorManageFieldsForm::constructTable()
WebformContentCreatorManageFieldsForm::form in src/Form/WebformContentCreatorManageFieldsForm.php
Gets the actual form array to be built.

File

src/Form/WebformContentCreatorManageFieldsForm.php, line 79

Class

WebformContentCreatorManageFieldsForm
Form handler for the Webform content creator manage fields form.

Namespace

Drupal\webform_content_creator\Form

Code

public function constructTable(array &$form) {
  $fieldTypesDefinitions = $this->pluginFieldType
    ->getDefinitions();
  $attributes = $this->entity
    ->getAttributes();
  $entity_type_initial_id = $this->entity
    ->getEntityTypeValue();
  $entity_type_id = $this->entity
    ->getEntityTypeValue();
  $bundle_id = $this->entity
    ->getBundleValue();
  if ($entity_type_id === 'node') {
    $entity_type_id = 'node_type';
  }
  $bundleFilteredFieldIds = WebformContentCreatorUtilities::getBundleIds($entity_type_initial_id, $bundle_id);
  asort($bundleFilteredFieldIds);
  $bundleFields = WebformContentCreatorUtilities::bundleFields($entity_type_initial_id, $bundle_id);
  $webform_id = $this->entity
    ->getWebform();
  $webformOptions = WebformContentCreatorUtilities::getWebformElements($webform_id);

  // Table header.
  $header = [
    self::BUNDLE_FIELD => $this
      ->t('Bundle field'),
    self::FIELD_TYPE => $this
      ->t('Field type'),
    self::CUSTOM_CHECK => $this
      ->t('Custom'),
    self::WEBFORM_FIELD => $this
      ->t('Webform field'),
    self::CUSTOM_VALUE => $this
      ->t('Custom text'),
  ];
  $form[self::FORM_TABLE] = [
    '#type' => 'table',
    '#header' => $header,
  ];
  foreach ($bundleFilteredFieldIds as $fieldId) {

    // Checkboxes with bundle fields.
    $form[self::FORM_TABLE][$fieldId][self::BUNDLE_FIELD] = [
      '#type' => 'checkbox',
      '#default_value' => array_key_exists($fieldId, $attributes),
      '#title' => $bundleFields[$fieldId]
        ->getLabel() . ' (' . $fieldId . ')',
    ];

    // Link to edit field settings.
    $form[self::FORM_TABLE][$fieldId][self::FIELD_TYPE] = [
      '#type' => 'markup',
      '#markup' => $fieldTypesDefinitions[$bundleFields[$fieldId]
        ->getType()]['label'],
    ];

    // Checkbox to select between webform element/property or custom text.
    $form[self::FORM_TABLE][$fieldId][self::CUSTOM_CHECK] = [
      '#type' => 'checkbox',
      '#default_value' => array_key_exists($fieldId, $attributes) ? $attributes[$fieldId][self::CUSTOM_CHECK] : '',
      '#states' => [
        'disabled' => [
          ':input[name="' . self::FORM_TABLE . '[' . $fieldId . '][' . self::BUNDLE_FIELD . ']"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $type = !empty($attributes[$fieldId]) && $attributes[$fieldId]['type'] ? '1' : '0';

    // Select with webform elements and basic properties.
    $form[self::FORM_TABLE][$fieldId][self::WEBFORM_FIELD] = [
      '#type' => 'select',
      '#options' => $webformOptions,
      '#states' => [
        'required' => [
          ':input[name="' . self::FORM_TABLE . '[' . $fieldId . '][' . self::BUNDLE_FIELD . ']"]' => [
            'checked' => TRUE,
          ],
          ':input[name="' . self::FORM_TABLE . '[' . $fieldId . '][' . self::CUSTOM_CHECK . ']"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    if (array_key_exists($fieldId, $attributes) && !$attributes[$fieldId][self::CUSTOM_CHECK]) {
      $form[self::FORM_TABLE][$fieldId][self::WEBFORM_FIELD]['#default_value'] = $type . ',' . $attributes[$fieldId][self::WEBFORM_FIELD];
    }

    // Textarea with custom text (including tokens)
    $form[self::FORM_TABLE][$fieldId][self::CUSTOM_VALUE] = [
      '#type' => 'textarea',
      '#default_value' => array_key_exists($fieldId, $attributes) ? $attributes[$fieldId][self::CUSTOM_VALUE] : '',
    ];
  }

  // Change table position in page.
  $form[self::FORM_TABLE]['#weight'] = 1;
}