You are here

public function AddViewsCustomTable::buildForm in Views Custom Table 9.0.x

Same name and namespace in other branches
  1. 8 src/Form/AddViewsCustomTable.php \Drupal\view_custom_table\Form\AddViewsCustomTable::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/AddViewsCustomTable.php, line 97

Class

AddViewsCustomTable
Add views custom table form.

Namespace

Drupal\view_custom_table\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  if ($this->step == 1) {
    $all_database_connections = Database::getAllConnectionInfo();
    foreach ($all_database_connections as $connection_name => $connection) {
      $displyName = $connection['default']['database'];
      $databaseOptions[$connection_name] = $displyName;
    }
    $form['table_database'] = [
      '#type' => 'select',
      '#options' => $databaseOptions,
      '#title' => $this
        ->t('Database'),
    ];
    $form['table_name'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Table'),
      '#required' => TRUE,
    ];
    $form['description'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Description'),
      '#rows' => 5,
      '#description' => $this
        ->t('Maximum 255 letters are allowed.'),
    ];
    $form['actions'] = [
      '#type' => 'actions',
    ];
    $form['actions']['next'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Next'),
    ];
  }
  elseif ($this->step == 2) {
    $table_name = $this->PreviousStepData['table_name'];
    $databaseName = $this->PreviousStepData['table_database'];
    $form['columns'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('"@table" Int Type Columns', [
        '@table' => $table_name,
      ]),
      '#tree' => TRUE,
    ];
    $entities['none'] = $this
      ->t('None');
    if ($databaseName == 'default') {
      $all_entities = $this->entityManager
        ->getDefinitions();
      foreach ($all_entities as $entity_name => $entity) {
        if ($entity
          ->getBaseTable()) {
          $entities[$entity_name] = $entity
            ->getLabel()
            ->render();
        }
      }
    }
    $all_custom_tables = $this->config
      ->getRawData();
    if (!empty($all_custom_tables)) {
      foreach ($all_custom_tables as $custom_table) {
        if ($custom_table['table_database'] == $databaseName && $custom_table['table_name'] != $table_name) {
          $entities[$custom_table->table_name] = $custom_table->table_name;
        }
      }
    }
    $int_types = [
      'tinyint',
      'smallint',
      'mediumint',
      'int',
      'bigint',
    ];
    $text_query = new FormattableMarkup('DESCRIBE @table_name', [
      '@table_name' => $table_name,
    ]);
    $connection = Database::getConnection('default', $databaseName);
    $query = $connection
      ->query($text_query);
    foreach ($query as $row) {
      $row_type = explode('(', $row->Type);
      if (in_array($row_type[0], $int_types)) {
        $form['columns']['column_' . $row->Field] = [
          '#type' => 'fieldset',
          '#title' => $this
            ->t('Relation of "@field_name" with', [
            '@field_name' => ucfirst($row->Field),
          ]),
          '#tree' => TRUE,
          '#attributes' => [
            'class' => [
              'container-inline',
            ],
          ],
        ];
        $form['columns']['column_' . $row->Field]['entity'] = [
          '#type' => 'select',
          '#title' => $this
            ->t('Entity'),
          '#options' => $entities,
        ];
        $form['columns']['column_' . $row->Field]['field'] = [
          '#type' => 'hidden',
          '#value' => $row->Field,
        ];
      }
    }
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Save'),
    ];
  }
  $form['actions']['cancel'] = [
    '#type' => 'link',
    '#title' => $this
      ->t('Cancel'),
    '#url' => $this
      ->buildCancelLinkUrl(),
  ];
  return $form;
}