Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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