You are here

public function EditTableRelations::buildForm in Views Custom Table 8

Same name and namespace in other branches
  1. 9.0.x src/Form/EditTableRelations.php \Drupal\view_custom_table\Form\EditTableRelations::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/EditTableRelations.php, line 72

Class

EditTableRelations
Edit views custom table form.

Namespace

Drupal\view_custom_table\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $table_name = NULL) {
  $config = $this->config
    ->getRawData();
  $table_relations = unserialize($config[$table_name]['column_relations']);
  $properties = [
    'base_table' => $table_name,
  ];
  $views = $this->entityManager
    ->getStorage('view')
    ->loadByProperties($properties);
  $form['table_name'] = [
    '#type' => 'hidden',
    '#value' => $table_name,
  ];
  $form['columns'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('"@table" Int Type Columns', [
      '@table' => $table_name,
    ]),
    '#tree' => TRUE,
  ];
  $entities['none'] = $this
    ->t('None');
  if ($config[$table_name]['table_database'] == 'default') {
    $all_entities = $this->entityManager
      ->getDefinitions();
    foreach ($all_entities as $entity_name => $entity) {
      if ($entity
        ->getBaseTable()) {
        $entities[$entity_name] = $entity
          ->getLabel()
          ->render();
      }
    }
  }
  if (!empty($config)) {
    foreach ($config as $table => $table_information) {
      $entities[$table] = $table;
    }
  }
  $int_types = [
    'tinyint',
    'smallint',
    'mediumint',
    'int',
    'bigint',
  ];
  $text_query = new FormattableMarkup('DESCRIBE @table_name', [
    '@table_name' => $table_name,
  ]);
  $connection = Database::getConnection('default', $config[$table_name]['table_database']);
  $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,
      ];
      if (!empty($views) && isset($table_relations[$row->Field])) {
        $form['columns']['column_' . $row->Field]['entity']['#disabled'] = TRUE;
      }
      if (isset($table_relations[$row->Field])) {
        $form['columns']['column_' . $row->Field]['entity']['#default_value'] = $table_relations[$row->Field];
        $form['columns']['column_' . $row->Field]['entity']['#description'] = $this
          ->t('"@table_name" is used in views, that is why this field con not be updated.', [
          '@table_name' => $table_name,
        ]);
      }
      $form['columns']['column_' . $row->Field]['field'] = [
        '#type' => 'hidden',
        '#value' => $row->Field,
      ];
    }
  }
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save'),
  ];
  $form['actions']['cancel'] = [
    '#type' => 'link',
    '#title' => $this
      ->t('Cancel'),
    '#url' => $this
      ->buildCancelLinkUrl(),
  ];
  return $form;
}