You are here

public function ShurlyEditForm::buildForm in ShURLy 8

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/ShurlyEditForm.php, line 44

Class

ShurlyEditForm
ShurlyActionsForm.

Namespace

Drupal\shurly\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $rid = NULL) {
  $shurly_link = \Drupal::database()
    ->query('SELECT * FROM {shurly} WHERE rid = :rid', [
    'rid' => $rid,
  ])
    ->fetchAllAssoc('rid');
  $shurly_history = \Drupal::database()
    ->query('SELECT * FROM {shurly_history} WHERE rid = :rid', [
    'rid' => $rid,
  ])
    ->fetchAll();
  $shurly_history_count = count($shurly_history);

  // Store the current values.
  $form_state
    ->setStorage([
    'shurly' => [
      'rid' => $rid,
      'source' => $shurly_link[$rid]->source,
      'count' => $shurly_link[$rid]->count,
      'destination' => urldecode($shurly_link[$rid]->destination),
    ],
  ]);
  if ($shurly_history) {
    $form['history'] = [
      '#prefix' => '<table>',
      '#suffix' => '</table>',
      '#tree' => TRUE,
    ];
    $form['history']['header'] = [
      '#markup' => '<thead>
        <tr>
          <th>' . t('Source') . '</th>
          <th>' . t('Changed') . '</th>
        </tr>
      </thead>',
    ];
    for ($i = 0; $i < $shurly_history_count; $i++) {
      $form['history']['row_' . $i] = [
        '#prefix' => '<tr class="' . ($i % 2 ? "odd" : "even") . '">',
        '#suffix' => '</tr>',
      ];
      $form['history']['row_' . $i]['destination'] = [
        '#prefix' => '<td>',
        '#suffix' => '</td>',
        '#markup' => link::fromTextAndUrl(Unicode::truncate($shurly_history[$i]->destination, 30), Url::fromUri($shurly_history[$i]->destination, [
          'attributes' => [
            'target' => [
              '_blank',
            ],
          ],
        ]))
          ->toString(),
      ];
      $form['history']['row_' . $i]['last_date'] = [
        '#prefix' => '<td>',
        '#suffix' => '</td>',
        '#markup' => \Drupal::service('date.formatter')
          ->format($shurly_history[$i]->last_date, 'short'),
      ];
    }
  }
  $form['source'] = [
    '#type' => 'textfield',
    '#title' => 'source',
    '#value' => $shurly_link[$rid]->source,
    '#disabled' => TRUE,
  ];
  $form['destination'] = [
    '#type' => 'textfield',
    '#title' => 'destination',
    '#value' => $shurly_link[$rid]->destination,
  ];
  $form['rid'] = [
    '#type' => 'hidden',
    '#value' => $rid,
  ];
  $form['count'] = [
    '#type' => 'hidden',
    '#value' => $shurly_link[$rid]->count,
  ];
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => 'Submit',
  ];
  return $form;
}