I have a couple things:
-A custom form
-A custom theme
-A custom module for this form
I am trying to get the custom form to display on a custom template which is in a custom theme.
structure for custom module called weeks:
weeks
-src
–Controller
—WeeksController.php
–Form
—FormWeeks.php
-weeks.info.yml
-weeks.install
-weeks.module
-weeks.routing.yml
this is what FormWeeks.php looks like
<?php
namespace DrupalweeksForm;
use DrupalCoreFormFormBase;
use DrupalCoreFormFormStateInterface;
class WeeksForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'weeks_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form('#theme') = 'weeks_form';
$form('deactivate') = array(
'#type' => 'submit',
'#value' => $this->t('Deactivate'),
'#name' => 'deactivate',
);
$form('2_week_pause') = array(
'#type' => 'submit',
'#value' => $this->t('Pause for 2 weeks'),
'#name' => 'pause'
);
$form('start_again_week_1') = array(
'#type' => 'submit',
'#title' => $this->t('Start again at week 1'),
'#name' => 'restart_week_1',
);
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$action = $form_state->getTriggeringElement()('#name');
$user = Drupal::currentUser();
if($action == 'deactivate'){
$user->set('field_52_weeks', $action);
$user->save();
}else if ($action == 'restart_week_1'){
$user->set('field_current_week', 1);
$user->save();
} else if ($action == '2_week_pause'){
$user->set('field_52_weeks', $action);
$user->save();
}
}
}
This is what is in weeks.module
<?php
function weeks_theme($existing, $type, $theme, $path){
return (
'weeks_form' => (
'render element' => 'form',
'template' => 'html--52weeks',
)
);
}
this is what weeks.routing.yml looks like
weeks.page:
path: '/52weeks'
defaults:
_title: '52 weeks'
_form: 'DrupalweeksFormWeeksForm'
requirements:
_permission: 'access content'
I have a twig file in themes/custom/mytheme/templates called html–52weeks.html.twig. This is whats in there:
{{ dump(form) }}
that ^^ returns null and I have no idea why. I’ve tried changing the $form(‘#theme’) = ‘mytheme’, in WeekForm.php, but this still doesnt work. What I want is for this form to be displayed when someone goes to the /52weeks url. The main problem is that the form does not get passed to the template