Skip to content

Instantly share code, notes, and snippets.

@prashantdsala
prashantdsala / customForm.php
Last active March 10, 2023 05:08
How to redirect from a Form using setRedirectUrl() method in Drupal
<?php
In the submitForm() method of your form class, create a new instance of the RedirectResponse class, which is used to redirect to a new URL.
// Use the setRedirectUrl() method to set the URL to which you want to redirect. You can pass the URL as a string or as an instance of the Url class.
// Return the RedirectResponse object from the submitForm() method to trigger the redirect.
// Here's an example of how to use setRedirectUrl() in a form class:
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RedirectDestinationInterface;
@prashantdsala
prashantdsala / custom_template.module
Created March 6, 2023 10:55
How to create a custom template file in Drupal
<?php
/**
* @file
* Primary module hooks for Custom Template module.
*/
/**
* Implements hook_theme().
*/
@prashantdsala
prashantdsala / CustomDrushCommandCommands.php
Created March 6, 2023 10:46
How to write a custom drush command in Drupal
<?php
namespace Drupal\custom_drush_command\Commands;
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Drush\Commands\DrushCommands;
/**
* A Drush commandfile.
*
@prashantdsala
prashantdsala / CustomControllerController.php
Created March 6, 2023 10:44
How to write a custom Controller in Drupal
<?php
namespace Drupal\custom_controller\Controller;
use Drupal\Core\Controller\ControllerBase;
/**
* Returns responses for Custom Controller routes.
*/
class CustomControllerController extends ControllerBase {
@prashantdsala
prashantdsala / SettingsForm.php
Last active March 6, 2023 10:42
How to create your own custom settings form in Drupal
<?php
namespace Drupal\custom_config_form\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Configure Custom Config Form settings for this site.
*/