You are here

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

Same name and namespace in other branches
  1. 8 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 100

Class

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

Namespace

Drupal\webform_content_creator\Form

Code

public function constructTable(array &$form) {
  $field_types_definitions = $this->pluginFieldType
    ->getDefinitions();
  $attributes = $this->entity
    ->getAttributes();
  $ct = $this->entity
    ->getContentType();
  $content_type = $this->entityTypeManager
    ->getStorage('node_type')
    ->load($ct);
  $node_filtered_field_ids = WebformContentCreatorUtilities::getContentFieldsIds($content_type);
  asort($node_filtered_field_ids);
  $node_fields = WebformContentCreatorUtilities::contentTypeFields($content_type);
  $webform_id = $this->entity
    ->getWebform();
  $webform_options = WebformContentCreatorUtilities::getWebformElements($webform_id);

  // Table header.
  $header = [
    self::CONTENT_TYPE_FIELD => $this
      ->t('Content type field'),
    self::FIELD_TYPE => $this
      ->t('Field type'),
    self::FIELD_MAPPING => $this
      ->t('Field mapping'),
    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,
  ];

  // Get the field mapping plugin manager.
  // $field_mapping_manager = \Drupal::service('plugin.manager.webform_content_creator.field_mappings');
  foreach ($node_filtered_field_ids as $field_id) {
    $route_parameters = [
      'node_type' => $ct,
      'field_config' => 'node.' . $ct . '.' . $field_id,
    ];
    $field_mapping_plugin = isset($attributes[$field_id][self::FIELD_MAPPING]) ? $attributes[$field_id][self::FIELD_MAPPING] : 'default_mapping';
    $selected_field_mapping = $this->fieldMappingManager
      ->getPlugin($field_mapping_plugin);

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

    // The field type.
    $node_field_type = $node_fields[$field_id]
      ->getType();

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

    // Find available field mappings for the element type.
    $field_mapping_options = [];
    foreach ($this->fieldMappingManager
      ->getFieldMappings($node_field_type) as $field_mapping) {
      $field_mapping_options[$field_mapping
        ->getId()] = $field_mapping
        ->getLabel();
    }

    // Select the field mapping.
    $default_value = array_key_exists($field_id, $attributes) && isset($attributes[$field_id][self::FIELD_MAPPING]) ? $attributes[$field_id][self::FIELD_MAPPING] : '';
    $form[self::FORM_TABLE][$field_id][self::FIELD_MAPPING] = [
      '#type' => 'select',
      '#options' => $field_mapping_options,
      '#default_value' => $default_value,
      '#states' => [
        'disabled' => [
          ':input[name="' . self::FORM_TABLE . '[' . $field_id . '][' . self::CONTENT_TYPE_FIELD . ']"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $component_fields = $field_mapping
      ->getEntityComponentFields($node_fields[$field_id]);
    $webform_options = $field_mapping
      ->getSupportedWebformFields($webform_id);
    if (sizeOf($component_fields) > 0) {
      foreach ($component_fields as $component_field) {
        $type = !empty($attributes[$field_id][$component_field]) && $attributes[$field_id][$component_field]['type'] ? '1' : '0';
        if (empty($attributes[$field_id][$component_field]) || $selected_field_mapping
          ->supportsCustomFields()) {
          $form[self::FORM_TABLE][$field_id][self::CUSTOM_CHECK][$component_field] = $this
            ->constructCustomCheck($field_id, $attributes, $component_field);
        }
        else {
          $form[self::FORM_TABLE][$field_id][self::CUSTOM_CHECK][$component_field] = [];
        }
        $form[self::FORM_TABLE][$field_id][self::WEBFORM_FIELD][$component_field] = $this
          ->constructWebformField($field_id, $webform_options, $attributes, $type, $component_field);
        if (empty($attributes[$field_id][$component_field]) || $selected_field_mapping
          ->supportsCustomFields()) {
          $form[self::FORM_TABLE][$field_id][self::CUSTOM_VALUE][$component_field] = $this
            ->constructCustomValue($field_id, $attributes, $component_field);
        }
        else {
          $form[self::FORM_TABLE][$field_id][self::CUSTOM_VALUE][$component_field] = [];
        }
      }
    }
    else {
      $type = !empty($attributes[$field_id]) && $attributes[$field_id]['type'] ? '1' : '0';
      if (empty($attributes[$field_id]) || $selected_field_mapping
        ->supportsCustomFields()) {
        $form[self::FORM_TABLE][$field_id][self::CUSTOM_CHECK] = $this
          ->constructCustomCheck($field_id, $attributes);
      }
      else {
        $form[self::FORM_TABLE][$field_id][self::CUSTOM_CHECK] = [];
      }
      $form[self::FORM_TABLE][$field_id][self::WEBFORM_FIELD] = $this
        ->constructWebformField($field_id, $webform_options, $attributes, $type);
      if (empty($attributes[$field_id]) || $selected_field_mapping
        ->supportsCustomFields()) {
        $form[self::FORM_TABLE][$field_id][self::CUSTOM_VALUE] = $this
          ->constructCustomValue($field_id, $attributes);
      }
      else {
        $form[self::FORM_TABLE][$field_id][self::CUSTOM_VALUE] = [];
      }
    }
  }

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