Skip to content

Instantly share code, notes, and snippets.

@BBGuy
Last active January 12, 2024 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BBGuy/a3d9b320f05345045e0b92b31e972fa9 to your computer and use it in GitHub Desktop.
Save BBGuy/a3d9b320f05345045e0b92b31e972fa9 to your computer and use it in GitHub Desktop.
<?php
// use a config in another module
$config = \Drupal::config('my_custom_module.my_config');
$var_value = $config->get('var_name');
<?php
public function buildForm(array $form, FormStateInterface $form_state) {
// If the form was submited the form build has access to the user entered data.
$user_input = $form_state->getUserInput();
$has_been_submitted = !empty($user_input);
// multiple submit buttons
$form['cases']['submit'] = [
'#type' => 'submit',
'#name' => 'submit',
'#value' => $this->t('Submit'),
];
if ($has_been_submitted) {
$form['cases']['reset'] = [
'#type' => 'submit',
'#name' => 'result',
'#value' => $this->t('Clear Form'),
];
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
// Get all values.
$values = $form_state->getValues();
// Get a single value.
$form_state->getValue('product_id');
// check what button was clicked
$action = $form_state->getTriggeringElement()['#name'];
\Drupal::messenger()->addMessage('$action=' . $action);
// Set the form to rebuild - othrwise all values entered will be lost.
$form_state->setRebuild();
}
<?php
public function buildForm(array $form, FormStateInterface $form_state) {
// See list of element types: https://api.drupal.org/api/drupal/elements/9.0.x
// see parant FormElement class https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21Element%21FormElement.php/class/FormElement/9.0.x
$form['fieldset_1'] = array(
'#type' => 'fieldset',
'#title' => $this->t('fieldset 1'),
);
// Collapsible fieldset - Replaced with HTML5 details elements
$form['advanced'] = array(
'#type' => 'details',
'#title' => t('Advanced settings'),
'#open' => FALSE,
);
// Text field
$form['text_field'] = [
'#type' => 'textfield',
'#title' => $this->t('Name'),
'#description' => $this->t('Enter your name.'),
'#maxlength' => 50,
'#size' => 50,
'#default_value' => 'Guy',
'#required' => TRUE,
'#disabled' = FALSE;
];
// password field
$form['password'] = [
'#type' => 'password',
'#title' => $this->t('Your password'),
'#description' => $this->t('Please enter your password'),
'#maxlength' => 25,
'#size' => 25,
'#weight' => '0',
];
// with config setting
$form['password'] = [
'#type' => 'password',
'#title' => $this->t('Your password'),
'#description' => $this->t('Please enter your password'),
'#maxlength' => $config->get('pg_catalogue_relation_type') ?? 25,
'#size' => 25,
'#weight' => '0',
];
// number
$form['line_number'] = [
'#type' => 'number',
'#title' => $this->t('Line number'),
'#weight' => '10',
];
// Markup
$form['markup_field'] = [
'#markup' => '<H1>This is a bit of HTML</H1>',
]
$form['checkbox_field'] = [
'#type' => 'checkbox',
'#title' => $this->t('Tick to agree to T&Cs.'),
'#default_value' => $log_all_requests,
];
// Select
$form['options'] = [
'#type' => 'select',
'#title' => $this->t('Options'),
'#options' => [1 => 'one', 2 => 'two', 3 => 'three'],
'#default_value' => 1,
];
// Options for an hierarchical select
'#options' => [
'1' => t('One'),
'Two' => [
'2.1' => t('Two point one'),
'2.2' => t('Two point two'),
],
'3' => t('Three'),
],
// Hidden field - will show in the source of the form but will not be displyed.
$form['hidden_id'] = array(
'#type' => 'hidden',
'#value' => 123,
);
// Value - esosated with the form but will not be in the source of the page.
$form['entity_id'] = array(
'#type' => 'value',
'#value' => $entity_id,
);
// Simple mark up
$form['head'] = [
'#markup' => '<H2>This is an H2</H2>',
];
// Advance markup using item
$form['html_with_label'] = [
'#type' => 'item',
'#title' => $this->t('Recipe'),
'#description' => $this->t('The recipe to base the new product on'),
'#markup' => $recipe_name,
'#prefix' => '<div id="field-recipe-name">',
'#suffix' => '</div>',
];
// Submit
$form['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Submit'),
];
// Date time - Time only
$form['import_time'] = [
'#type' => 'datetime',
'#title' => $this->t('Import time'),
'#date_date_element' => 'none',
'#date_time_format' => 'H:i:s',
'#date_time_element' => 'HTML Time',
'#default_value' => $config_import_time,
];
// Date field
$form['archive_date'] = [
'#type' => 'date',
'#title' => $this->t('Archive date'),
// Default to 1 year ago.
'#default_value' => date('Y-m-d', time() - 31536000) ,
];
// Date list - example showing - Year set as text all other elements use dropdown
$form['datelist_element'] = [
'#title' => 'datelist test',
'#type' => 'datelist',
//'#default_value' => new DrupalDateTime('2000-01-01 00:00:00'),
'#date_part_order' => [
'month',
'day',
'year',
'hour',
'minute', 'ampm',
],
'#date_text_parts' => ['year'],
'#date_year_range' => '2010:2020',
'#date_increment' => 15,
];
$form['datetime_element_html'] = [
'#title' => 'HTML Datetime',
'#type' => 'datetime',
'#date_date_format' => 'Y-m-d',
'#date_time_format' => 'H:i:s',
'#date_date_element' => 'HTML Date',
'#date_time_element' => 'HTML Time',
'#date_increment' => 1,
];
$form['author'] = array(
'#type' => 'fieldset',
'#title' => $this->t('Author'),
'#attributes' => ['class' => ['tile', 'tile--subtle', 'tile--rounded']],
);
$form['author']['name'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Name'),
);
// Conditional fields
$form['colour_select'] = [
'#type' => 'radios',
'#title' => $this->t('Pick a colour'),
'#options' => [
'red' => $this->t('White'),
'other' => $this->t('Other'),
],
];
$form['custom_colour'] = [
'#type' => 'textfield',
'#size' => '60',
'#placeholder' => 'Enter favourite colour',
'#states' => [
'visible' => [
':input[name="colour_select"]' => ['value' => 'other'],
],
],
];
// Add CSS.
$form['#attached']['html_head'][] = [
'#tag' => 'style',
'#value' => '.content__inner ul { text-align: left; }' ,
];
return $form;
}
<?php
// vertical tabs - https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21Element%21VerticalTabs.php/class/VerticalTabs/
$form['information'] = array(
'#type' => 'vertical_tabs',
'#default_tab' => 'edit-publication',
);
$form['author'] = array(
'#type' => 'details',
'#title' => $this
->t('Author'),
'#group' => 'information',
);
$form['author']['name'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Name'),
);
$form['publication'] = array(
'#type' => 'details',
'#title' => $this
->t('Publication'),
'#group' => 'information',
);
$form['publication']['publisher'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Publisher'),
);
<?php
/**
* Implements hook_form_alter().
* Template
*/
function my_module_form_alter(&$form, FormStateInterface $form_state, $form_id) {
\Drupal::messenger()->addMessage("form_id = $form_id", 'status');
if ($form_id == 'xxxxxx') {
// Do stuff
}
}
/**
* Modify the address of a contact us form.
*/
function my_module_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if ($form_id == 'contact_message_contact_us_form') {
$available_country_codes = ['GB','US','FR'];
$form['field_contact_country']['widget'][0]['value']['#available_countries'] = $available_country_codes;
$form['field_contact_country']['widget'][0]['value']['#default_value'] = 'GB';
}
}
// After build - Template
function my_module_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$form['#after_build'][] = 'my_module_after_build';
// Add a validate
$form['#validate'][] = 'my_module_form_validate';
// Add a submit that will run before the other submits
array_unshift($form['actions']['submit']['#submit'] , 'my_module_form_submit');
}
function my_module_after_build($form, &$form_state) {
// do stuff to the form.
return $form;
}
function my_module_form_validate(array $form, FormStateInterface $form_state) {
$values = $form_state->getValues();
$selected_country = $form_state->getValue('field_x');
}
function my_module_form_submit(array $form, FormStateInterface $form_state) {
}
<?php
// List of statuses that can be set
visible
invisible
required
enabled
disabled
// Condition types
// Checkbox
['checked' => TRUE]
// Select
['value' => 'option key']
// Or conditions
'#states' => [
'disabled' => [
[':input[name="checkbox1"]' => ['checked' => TRUE]],
'or',
[':input[name="checkbox2"]' => ['checked' => TRUE]],
],
],
// And conditions
'required' => [
[
[':input[name="checkbox1"]' => ['checked' => TRUE]],
[':input[name="checkbox2"]' => ['checked' => TRUE]],
],
// Multiple actions
'#states' => [
'visible' => [
[
[':input[name="checkbox1"]' => ['checked' => TRUE]],
],
],
'required' => [
[
[':input[name="checkbox2"]' => ['checked' => TRUE]],
],
],
],
// Examples
// Controlled by a checkbox
$form['toggle_disable'] = [
'#type' => 'checkbox',
'#title' => $this->t('Override last run'),
'#default_value' => FALSE,
];
// Fieldsets can be used to controll the state of its elements
// also useful if a type like datetime #states' does not work for some reason.
'#type' => 'fieldset',
//'#title' => $this->t('Fieldsset 1'),
'#states' => [
'disabled' => [
':input[name="override_last_run"]' => ['checked' => TRUE],
],
],
'#type' => 'fieldset',
//'#title' => $this->t('Fieldsset 2'),
'#states' => [
'enabled' => [
':input[name="override_last_run"]' => ['checked' => TRUE],
],
],
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment