If you still have problems, please let us know, by sending an email to email@example.com Thank you!
In Drupal 8 it is easy to attach a cs/js to a template file.
We need to define a library that will contain the js/css files needed.
The libraries are defined in the file mymodule.libraries.xml
There, we define the each library with their js, css and dependencies of other libraries.
In this tutorial I will show you a quick implementation of hook_theme, that will list the title/body of a FAQ nodes.
I've created a new routing and a new controller class (FaqController) for this.
mymodule.faq_controller_faq: path: 'faq' defaults: _controller: '\Drupal\mymodule\Controller\FaqController::faq' _title: 'FAQ' requirements: _permission: 'access content'
Because I want to get all FAQ nodes, I'm injecting the entity_type.manager service into our controller class.
To create a configuration form, we need to extend our form from ConfigFormBase and not from FormBase like we do in case of a normal form.
Extending the ConfigFormBase, we need also to implement the getEditableConfigNames() method, that will return an array with condiguration names that are editable.
In the buildForm() method, we need to create all form elements that will be used to input the settings. For this, we can use the form API of Drupal 8.
Of course, we can have access to our configuration object and get all our configurations from it.
Similar to Drupal 7, we are able to pass custom data between form validate/submit callbacks.
In Drupal 8, there are two way of setting a key => value to $form_state: set a single key => value or multiple keys => values.
To set a single key, we can use set() method that is defined in FormStateInterface interface.
In order to attach custom validate/submit callbacks to an existing form in Drupal 8, you need to alter it.
To alter an existing form, we need to use the hook_form_alter() function.
Let's suppose for example that we have an existing form called example_form defined in a module example.
An we have a module called mymodule, where we want to alter the example_form and add our custom validate/submit callbacks.
The first step is to implement the hook_form_alter() in your mymodule.module file.
In this tutorial I will show you how to create a simple form using Drupal 8.
In order to create a new form, we need to extend the FormBase class. The FormBase class is implementing the FormInterface interface, so our class needs to implement the methods from FormInterface.
The place where we create forms in Drupal 8 is: /mymodule/src/Form
The class name should end in 'Form', for example: ExampleForm.
The methods from FormInterface are:
In this 'How to' I will show you how to subscribe to an exisitng event.
You can read my previous article on How to dispatch an event.
1 Create a new file mymodule.services.yml in root if your module
In this file, we need to create a new service and tag it as an 'event_subscriber' so Drupal will know that this service is an event subscriber.
In this 'How to' I will show you how to dispatch an event from a module called 'othermodule'.
First, we need a class that is extending the \Symfony\Component\EventDispatcher\Event class.
This calss will contains all the information we want to send when we dispatch the event.
Place this class in \othermodule\src\Event\OtherModuleEvent.php