You are here

protected function YamlFormCompositeBase::buildCompositeElementsTable in YAML Form 8

Build the composite elements settings table.

Return value

array A renderable array container the composite elements settings table.

1 call to YamlFormCompositeBase::buildCompositeElementsTable()
YamlFormCompositeBase::form in src/Plugin/YamlFormElement/YamlFormCompositeBase.php
Gets the actual configuration form array to be built.
1 method overrides YamlFormCompositeBase::buildCompositeElementsTable()
YamlFormLocation::buildCompositeElementsTable in src/Plugin/YamlFormElement/YamlFormLocation.php
Build the composite elements settings table.

File

src/Plugin/YamlFormElement/YamlFormCompositeBase.php, line 255

Class

YamlFormCompositeBase
Provides a base for composite elements.

Namespace

Drupal\yamlform\Plugin\YamlFormElement

Code

protected function buildCompositeElementsTable() {
  $header = [
    $this
      ->t('Key'),
    $this
      ->t('Title/Description/Placeholder'),
    $this
      ->t('Type/Options'),
    $this
      ->t('Required'),
    $this
      ->t('Visible'),
  ];
  $rows = [];
  $composite_elements = $this
    ->getCompositeElements();
  foreach ($composite_elements as $composite_key => $composite_element) {
    $title = isset($composite_element['#title']) ? $composite_element['#title'] : $composite_key;
    $type = isset($composite_element['#type']) ? $composite_element['#type'] : NULL;
    $t_args = [
      '@title' => $title,
    ];
    $attributes = [
      'style' => 'width: 100%; margin-bottom: 5px',
    ];
    $state_disabled = [
      'disabled' => [
        ':input[name="properties[' . $composite_key . '__access]"]' => [
          'checked' => FALSE,
        ],
      ],
    ];
    $row = [];

    // Key.
    $row[$composite_key . '__key'] = [
      '#markup' => $composite_key,
      '#access' => TRUE,
    ];

    // Title, placeholder, and description.
    if ($type) {
      $row['title_and_description'] = [
        'data' => [
          $composite_key . '__title' => [
            '#type' => 'textfield',
            '#title' => $this
              ->t('@title title', $t_args),
            '#title_display' => 'invisible',
            '#placeholder' => $this
              ->t('Enter title...'),
            '#attributes' => $attributes,
            '#states' => $state_disabled,
          ],
          $composite_key . '__placeholder' => [
            '#type' => 'textfield',
            '#title' => $this
              ->t('@title placeholder', $t_args),
            '#title_display' => 'invisible',
            '#placeholder' => $this
              ->t('Enter placeholder...'),
            '#attributes' => $attributes,
            '#states' => $state_disabled,
          ],
          $composite_key . '__description' => [
            '#type' => 'textarea',
            '#title' => $this
              ->t('@title description', $t_args),
            '#title_display' => 'invisible',
            '#rows' => 2,
            '#placeholder' => $this
              ->t('Enter description...'),
            '#attributes' => $attributes,
            '#states' => $state_disabled,
          ],
        ],
      ];
    }
    else {
      $row['title_and_description'] = [
        'data' => [
          '',
        ],
      ];
    }

    // Type and options.
    // Using if/else instead of switch/case because of complex conditions.
    $row['type_and_options'] = [];
    if ($type == 'tel') {
      $row['type_and_options']['data'][$composite_key . '__type'] = [
        '#type' => 'select',
        '#required' => TRUE,
        '#options' => [
          'tel' => $this
            ->t('Telephone'),
          'textfield' => $this
            ->t('Text field'),
        ],
        '#attributes' => [
          'style' => 'width: 100%; margin-bottom: 5px',
        ],
        '#states' => $state_disabled,
      ];
    }
    elseif ($type == 'select' && ($composite_options = $this
      ->getCompositeElementOptions($composite_key))) {
      $row['type_and_options']['data'][$composite_key . '__type'] = [
        '#type' => 'select',
        '#required' => TRUE,
        '#options' => [
          'select' => $this
            ->t('Select'),
          'yamlform_select_other' => $this
            ->t('Select other'),
          'textfield' => $this
            ->t('Text field'),
        ],
        '#attributes' => [
          'style' => 'width: 100%; margin-bottom: 5px',
        ],
        '#states' => $state_disabled,
      ];
      $row['type_and_options']['data'][$composite_key . '__options'] = [
        '#type' => 'select',
        '#options' => $composite_options,
        '#required' => TRUE,
        '#attributes' => [
          'style' => 'width: 100%;',
        ],
        '#states' => $state_disabled + [
          'invisible' => [
            ':input[name="properties[' . $composite_key . '__type]"]' => [
              'value' => 'textfield',
            ],
          ],
        ],
      ];
    }
    else {
      $row['type_and_options']['data'][$composite_key . '__type'] = [
        '#type' => 'textfield',
        '#access' => FALSE,
      ];
      $row['type_and_options']['data']['markup'] = [
        '#markup' => $this->elementManager
          ->getElementInstance($composite_element)
          ->getPluginLabel(),
        '#access' => TRUE,
      ];
    }

    // Required.
    if ($type) {
      $row[$composite_key . '__required'] = [
        '#type' => 'checkbox',
        '#return_value' => TRUE,
      ];
    }
    else {
      $row[$composite_key . '__required'] = [
        'data' => [
          '',
        ],
      ];
    }

    // Access.
    $row[$composite_key . '__access'] = [
      '#type' => 'checkbox',
      '#return_value' => TRUE,
    ];
    $rows[$composite_key] = $row;
  }
  return [
    '#type' => 'table',
    '#header' => $header,
  ] + $rows;
}