Example #1
0
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state, RouteMatch $routeMatch = NULL)
 {
     $pbid = NULL;
     if ($routeMatch->getRouteName() == 'd8phonebook.edit') {
         // If we're on an edit URL, try to retrieve the phonebook entry by its ID.
         $pbid = $routeMatch->getParameter('phonebook');
         $query = $this->connection->select('phonebook', 'p')->fields('p');
         $query->condition('pbid', $pbid);
         $entry = $query->execute()->fetchObject();
         if (!$entry) {
             // Return 404 if the entry is not found.
             throw new NotFoundHttpException();
         }
     }
     // Push the phonebook entry ID to the submit handler.
     $form['pbid'] = ['#type' => 'value', '#value' => $pbid];
     $form['name'] = ['#type' => 'textfield', '#title' => $this->t('Name'), '#maxlength' => 64, '#default_value' => $pbid ? $entry->name : ''];
     $form['phone'] = ['#type' => 'textfield', '#title' => $this->t('Phone'), '#maxlength' => 64, '#default_value' => $pbid ? $entry->phone : ''];
     $form['actions'] = ['#type' => 'actions', 'save' => ['#type' => 'submit', '#value' => $this->t('Save')]];
     return $form;
 }