Drupal 8 comes with a great addition to the backend developer toolkit in the form of the plugin system. Completely new, specific to Drupal and evolved from serving only a few specific purposes, plugins have become the go-to system for reusable functionality in Drupal 8.
In this article series of two parts, we will use this system to build a feature that allows the use of custom forms together with node entities. After we’re done, we’ll be able to do the following:
- configure node bundles to use one of multiple form types to be displayed together with the node display
- easily define new form types by extending from a sensible base class
Because the topic is very well covered elsewhere, I will not go into the details of how plugins work. But do feel free to brush up on the theory before diving into the crux of it here. And if you want to take a look at the end result, the code we write in both articles can be found in this repository.
We will get started by creating our custom plugin type. To this end, we will have 2 interfaces and 6 classes. It sounds like much, but I assure you they are rather boilerplate and quick to set up. Then, in the next installment of this series, we will see how to use it for our reusable forms attached to nodes.
Plugin manager
Responsible for discovering and loading plugins, the most important part in any plugin type is the manager. However, it’s very simple to create one because Drupal already provides us with a sensible default base to extend. So in our module’s /src
folder we can have this class inside a ReusableFormManager.php
file (the de facto name of our plugin type becoming ReusableForm
):
<?php
namespace Drupal\reusable_forms;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
class ReusableFormsManager extends DefaultPluginManager {
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/ReusableForm', $namespaces, $module_handler, 'Drupal\reusable_forms\ReusableFormPluginInterface', 'Drupal\reusable_forms\Annotation\ReusableForm');
$this->alterInfo('reusable_forms_info');
$this->setCacheBackend($cache_backend, 'reusable_forms');
}
}
As I mentioned, our manager extends the DefaultPluginManager
class and just overrides the constructor to call the parent one with some important information about our plugin type:
Plugin/ReusableForm
– the subdirectory where plugins of this type will be found within any moduleDrupal\reusable_forms\ReusableFormPluginInterface
– the interface each of our plugins will need to implementDrupal\reusable_forms\Annotation\ReusableForm
– the annotation class that will define our plugin properties (such as ID, name, etc.)
Additionally, we create an alter hook which can be implemented by various modules to alter the plugin definitions and we set a key for our plugins in the cache backend. For more information about plugin managers, what they do and how they are set up, you should consult the documentation page available on Drupal.org.
Plugin interface
Next, let’s create that interface the manager expects all our plugins to implement. Inside a file called ReusableFormPluginInterface.php
located in the src/
folder of our module, we can have this:
<?php
namespace Drupal\reusable_forms;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Component\Plugin\PluginInspectionInterface;
interface ReusableFormPluginInterface extends PluginInspectionInterface, ContainerFactoryPluginInterface {
/**
* Return the name of the reusable form plugin.
*
* @return string
*/
public function getName();
/**
* Builds the associated form.
*
* @param $entity EntityInterface.
* The entity this plugin is associated with.
*
* @return array().
* Render array of form that implements \Drupal\reusable_forms\Form\ReusableFormInterface
*/
public function buildForm($entity);
}
Read More 4501
Read More 4502
Read More 4503
Read More 4504
Read More 4505
Read More 4506
Read More 4507
Read More 4508
Read More 4509
Read More 4510
Read More 4511
Read More 4512
Read More 4513
Read More 4514
Read More 4515
Read More 4516
Read More 4517
Read More 4518
Read More 4519
Read More 4520
Read More 4521
Read More 4522
Read More 4523
Read More 4524
Read More 4525
Read More 4526
Read More 4527
Read More 4528
Read More 4529
Read More 4530
Read More 4531
Read More 4532
Read More 4533
Read More 4534
Read More 4535
Read More 4536
Read More 4537
Read More 4538
Read More 4539
Read More 4540
Read More 4541
Read More 4542
Read More 4543
Read More 4544
Read More 4545
Read More 4546
Read More 4547
Read More 4548
Read More 4549
Read More 4550
Read More 4551
Read More 4552
Read More 4553
Read More 4554
Read More 4555
Read More 4556
Read More 4557
Read More 4558
Read More 4559
Read More 4560
Read More 4561
Read More 4562
Read More 4563
Read More 4564
Read More 4565
Read More 4566
Read More 4567
Read More 4568
Read More 4569
Read More 4570
Read More 4571
Read More 4572
Read More 4573
Read More 4574
Read More 4575
Read More 4576
Read More 4577
Read More 4578
Read More 4579
Read More 4580
Read More 4581
Read More 4582
Read More 4583
Read More 4584
Read More 4585
Read More 4586
Read More 4587
Read More 4588
Read More 4589
Read More 4590
Read More 4591
Read More 4592
Read More 4593
Read More 4594
Read More 4595
Read More 4596
Read More 4597
Read More 4598
Read More 4599
Read More 4600
Read More 4601
Read More 4602
Read More 4603
Read More 4604
Read More 4605
Read More 4606
Read More 4607
Read More 4608
Read More 4609
Read More 4610
Read More 4611
Read More 4612
Read More 4613
Read More 4614
Read More 4615
Read More 4616
Read More 4617
Read More 4618
Read More 4619
Read More 4620
Read More 4621
Read More 4622
Read More 4623
Read More 4624
Read More 4625
Read More 4626
Read More 4627
Read More 4628
Read More 4629
Read More 4630
Read More 4631
Read More 4632
Read More 4633
Read More 4634
Read More 4635
Read More 4636
Read More 4637
Read More 4638
Read More 4639
Read More 4640
Read More 4641
Read More 4642
Read More 4643
Read More 4644
Read More 4645
Read More 4646
Read More 4647
Read More 4648
Read More 4649
Read More 4650
Read More 4651
Read More 4652
Read More 4653
Read More 4654
Read More 4655
Read More 4656
Read More 4657
Read More 4658
Read More 4659
Read More 4660
Read More 4661
Read More 4662
Read More 4663
Read More 4664
Read More 4665
Read More 4666
Read More 4667
Read More 4668
Read More 4669
Read More 4670
Read More 4671
Read More 4672
Read More 4673
Read More 4674
Read More 4675
Read More 4676
Read More 4677
Read More 4678
Read More 4679
Read More 4680
Read More 4681
Read More 4682
Read More 4683
Read More 4684
Read More 4685
Read More 4686
Read More 4687
Read More 4688
Read More 4689
Read More 4690
Read More 4691
Read More 4692
Read More 4693
Read More 4694
Read More 4695
Read More 4696
Read More 4697
Read More 4698
Read More 4699
Read More 4700
Read More 4701
Read More 4702
Read More 4703
Read More 4704
Read More 4705
Read More 4706
Read More 4707
Read More 4708
Read More 4709
Read More 4710
Read More 4711
Read More 4712
Read More 4713
Read More 4714
Read More 4715
Read More 4716
Read More 4717
Read More 4718
Read More 4719
Read More 4720
Read More 4721
Read More 4722
Read More 4723
Read More 4724
Read More 4725
Read More 4726
Read More 4727
Read More 4728
Read More 4729
Read More 4730
Read More 4731
Read More 4732
Read More 4733
Read More 4734
Read More 4735
Read More 4736
Read More 4737
Read More 4738
Read More 4739
Read More 4740
Read More 4741
Read More 4742
Read More 4743
Read More 4744
Read More 4745
Read More 4746
Read More 4747
Read More 4748
Read More 4749
Read More 4750
Read More 4751
Read More 4752
Read More 4753
Read More 4754
Read More 4755
Read More 4756
Read More 4757
Read More 4758
Read More 4759
Read More 4760
Read More 4761
Read More 4762
Read More 4763
Read More 4764
Read More 4765
Read More 4766
Read More 4767
Read More 4768
Read More 4769
Read More 4770
Read More 4771
Read More 4772
Read More 4773
Read More 4774
Read More 4775
Read More 4776
Read More 4777
Read More 4778
Read More 4779
Read More 4780
Read More 4781
Read More 4782
Read More 4783
Read More 4784
Read More 4785
Read More 4786
Read More 4787
Read More 4788
Read More 4789
Read More 4790
Read More 4791
Read More 4792
Read More 4793
Read More 4794
Read More 4795
Read More 4796
Read More 4797
Read More 4798
Read More 4799
Read More 4800
Read More 4801
Read More 4802
Read More 4803
Read More 4804
Read More 4805
Read More 4806
Read More 4807
Read More 4808
Read More 4809
Read More 4810
Read More 4811
Read More 4812
Read More 4813
Read More 4814
Read More 4815
Read More 4816
Read More 4817
Read More 4818
Read More 4819
Read More 4820
Read More 4821
Read More 4822
Read More 4823
Read More 4824
Read More 4825
Read More 4826
Read More 4827
Read More 4828
Read More 4829
Read More 4830
Read More 4831
Read More 4832
Read More 4833
Read More 4834
Read More 4835
Read More 4836
Read More 4837
Read More 4838
Read More 4839
Read More 4840
Read More 4841
Read More 4842
Read More 4843
Read More 4844
Read More 4845
Read More 4846
Read More 4847
Read More 4848
Read More 4849
Read More 4850
Read More 4851
Read More 4852
Read More 4853
Read More 4854
Read More 4855
Read More 4856
Read More 4857
Read More 4858
Read More 4859
Read More 4860
Read More 4861
Read More 4862
Read More 4863
Read More 4864
Read More 4865
Read More 4866
Read More 4867
Read More 4868
Read More 4869
Read More 4870