You are here

public function WebformContentCreatorManageFieldsForm::constructTable in Webform Content Creator 8

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

Constructs table with mapping between webform and content type.

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 88

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();
  $ct = $this->entity
    ->getContentType();
  $contentType = $this->entityTypeManager
    ->getStorage('node_type')
    ->load($ct);
  $nodeFilteredFieldIds = WebformContentCreatorUtilities::getContentFieldsIds($contentType);
  asort($nodeFilteredFieldIds);
  $nodeFields = WebformContentCreatorUtilities::contentTypeFields($contentType);
  $webform_id = $this->entity
    ->getWebform();
  $webformOptions = WebformContentCreatorUtilities::getWebformElements($webform_id);

  // Table header.
  $header = [
    self::CONTENT_TYPE_FIELD => $this
      ->t('Content type 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 ($nodeFilteredFieldIds as $fieldId) {
    $route_parameters = [
      'node_type' => $ct,
      'field_config' => 'node.' . $ct . '.' . $fieldId,
    ];

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

    // Link to edit field settings.
    $form[self::FORM_TABLE][$fieldId][self::FIELD_TYPE] = [
      '#type' => 'link',
      '#title' => $fieldTypesDefinitions[$nodeFields[$fieldId]
        ->getType()]['label'],
      '#url' => Url::fromRoute("entity.field_config.node_storage_edit_form", $route_parameters),
      '#options' => [
        'attributes' => [
          'title' => $this
            ->t('Edit field settings.'),
        ],
      ],
    ];

    // 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::CONTENT_TYPE_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::CONTENT_TYPE_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;
}