I'm trying to process my custom form. However, code inside the submitForm function is never called. I have not seen any solutions, yet for getting Drupal 8's submitForm to be called.
namespace Drupal\archive\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
class DateForm extends FormBase
{
public function getFormId() { return 'archive_dateform'; }
public function buildForm(array $form, FormStateInterface $form_state)
{
$form['#action'] = Url::fromRoute('archive.dateform')->toString();
$form['type'] = [
'#type' => 'hidden',
'#value' => $form_state->get('type')
];
$form['year'] = [
'#title' => 'Year',
'#type' => 'number',
'#size' => 4,
'#step' => 1,
'#default_value' => $form_state->get('year'),
];
$options = [];
for ($i=1; $i<=12; $i++) { $options[$i] = date('F', mktime(0, 0, 0, $i, 10)); }
$form['month'] = [
'#title' => 'Month',
'#type' => 'select',
'#options' => $options,
'#default_value' => $form_state->get('month'),
'#empty_option' => 'All',
'#empty_value' => 0
];
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => 'Search',
'#button_type' => 'primary'
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state)
{
$type = $form_state->get('type');
if (!$type) {
$form_state->setErrorByName('type', 'Missing Type');
}
}
public function submitForm(array &$form, FormStateInterface $form_state)
{
echo "submitForm\n";
exit();
$type = $form_state->get('type');
$year = (int)$form_state->get('year');
if (!$year) { $year = (int)date('Y'); }
$params = ['year'=>$year];
$month = (int)$form_state->get('month');
if ($month) { $params['month'] = $month; }
$form_state->setRedirect("archive.$type", $params);
return;
}
}
And using from a controller
namespace Drupal\archive\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Form\FormState;
class ArchiveController extends ControllerBase
{
public function archive($type, $year, $month, $day)
{
$year = (int)$year;
$month = (int)$month;
$day = (int)$day;
if (!$year) { $year = (int)date('Y'); }
if (!$month) {
$date = "$year-01-01";
$period = new \DateInterval('P1Y1D');
}
else {
if (!$day) {
$date = "$year-$month-01";
$period = new \DateInterval('P1M1D');
}
else {
$date = "$year-$month-$day";
$period = new \DateInterval('P1D');
}
}
$start = new \DateTime($date);
$end = new \DateTime($date);
$end->add($period);
$manager = \Drupal::entityTypeManager();
$query = \Drupal::entityQuery('node')
->condition('type', $type)
->condition('created', $start->format('U'), '>=')
->condition('created', $end ->format('U'), '<')
->condition('status', 1);
$form_state = new FormState();
$form_state->set('type', $type);
$form_state->set('year', $year);
$form_state->set('month', $month);
$form = \Drupal::formBuilder()->buildForm('Drupal\archive\Form\DateForm', $form_state);
return [
'#theme' => 'archive_results',
'#year' => $year,
'#start' => $start,
'#form' => $form,
'#results' => $manager->getViewBuilder('node')->viewMultiple(
$manager->getStorage('node')->loadMultiple($query->execute()),
'teaser'
)
];
}
}
drupal_set_message("submit");
and remove the call to exit, and you will likely see the submit handler is called.