Skip to content

Instantly share code, notes, and snippets.

@hussainweb
Created April 24, 2021 03:18
Show Gist options
  • Save hussainweb/e57e1fafc2c2dfcc2b5621234948622f to your computer and use it in GitHub Desktop.
Save hussainweb/e57e1fafc2c2dfcc2b5621234948622f to your computer and use it in GitHub Desktop.
<?php
// in src/Form directory.
namespace Drupal\mymodule\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\system\Form\SiteInformationForm;
/**
* Extends drupal core SiteInformationForm, Add a new text field to save api
* key for our site.
*
* Class ExtendedSiteInformationForm
*
* @package Drupal\api_settings\Form
*/
class ExtendedSiteInformationForm extends SiteInformationForm {
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// Build parent form.
$form = parent::buildForm($form, $form_state);
// Add siteapikey text box to site information group.
$form['new_element'] = [
'#type' => 'textfield',
// ... more attributes
];
return $form;
}
}
services:
mymodule.route_subscriber:
class: Drupal\mymodule\Routing\RouteSubscriber
tags:
- { name: event_subscriber }
<?php
// in src/Routing directory.
namespace Drupal\mymodule\Routing;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;
/**
* Provides a routing subscriber which change _form key of
* "system.site_information_settings" route to override that form. Class
*
* Class RouteSubscriber
*
* @package Drupal\api_settings\Routing
*/
class RouteSubscriber extends RouteSubscriberBase {
/**
* {@inheritdoc}
*/
protected function alterRoutes(RouteCollection $collection) {
if ($route = $collection->get('system.site_information_settings')) {
$route->setDefault('_form', '\Drupal\mymodule\Form\ExtendedSiteInformationForm');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment