Hot answers tagged

18

Put the keys in an array, for example: $this->configuration['header']['margin-top-submit'] = $form_state->getValue(['header','margin-top']);


12

The form element property #type refers to an element type, not a HTML element. It is okay to define your own element types (or theme types), and you can provide your own markup for it. This can be done by implementing a RenderElement plugin. Update: You can also use the datetime render element and disable the date portion. $element['#date_date_element'] = ...


11

This issue I ran into was I did not specify '#theme' in the Block Plugin. I added public function build() { return array( '#theme' => 'myplugin', '#title' => 'Test', '#color' => $this->configuration['color'], ); } and it worked


11

You can use the plugin manager to fetch a plugin. You only need to find the correct plugin manager service: $type = \Drupal::service('plugin.manager.service'); // Fill your own plugin manager service. $plugin_definition = $type->getDefinition('plugin_id'); // Fill needed plugin id.


9

Action plugin yaml definitions need to be prefixed with the name system.action.action_name.yml, and not mymodule.action_name.yml. For reference, check the provided action plugins for node and user module in core in their config/install and config/schema folders. Uninstall the module, rename the files, and install the module again so the changes are picked ...


8

The easy solution for this, is the add a new base field on the menu link entity, the Drupal the rest for you. You can do this with the hook_base_field_info() hook. This code should work, but you might need to tweak it. /** * Implements hook_entity_base_field_info(). */ function MODULE_entity_base_field_info(\Drupal\Core\Entity\EntityTypeInterface $...


7

An example for a field formatter plugin with dependency injections. This works differently from services. First parameter injected is the container to get all services needed. Drupal\Core\Field\Plugin\Field\FieldFormatter class StringFormatter extends FormatterBase implements ContainerFactoryPluginInterface { /** * The entity type manager. * * @...


6

Yes, you're right: Modules are still modules, the equivalent of what WordPress calls plugins. Each module can implement one or more plugins. For example, the Voting API module has a plugin for each of the methods to show the result: Average, Sum and Count. Plugins also needs a plugin manager, which is the responsible for actually calling the plugins. You ...


6

Method #1 You can simply create new plugin which extends class of target Core plugin. File: modules/example/src/Plugin/EntityReferenceSelection/ExampleSelection.php namespace Drupal\example\Plugin\EntityReferenceSelection; use Drupal\Component\Utility\Html; use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection; /** * Entity ...


6

To be exact on your question: there's no difference in internal implementation. But as a best practice, you should be using $this->t instead of the global t wherever possible. If you check the function, t is returning a new TranslateableMarkup object. If your class says $this->t is not defined, you can add the StringTranslationTrait to add the ...


6

It is not modules vs. plugins. Plugins are competing against hooks, which they will eventually replace entirely, together with event subscribers and services. By the way, all are implemented by modules because you are not allowed to insert them in core directly.


5

What you want to do, is to make views use your class instead of the the one solr module provides. Defining your own plugin wont help. Instead you can use hook_views_plugins_query_alter to specify the plugin class.


5

This worked for me in drupal 8 after many hours of testing. I hope it helps. public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { $is_live = $form_state->getValue('settings')['is_live ']; }


5

A configurable or a base field? Either way, that's a perfect use case for using a list (string) field type, then you tell Drupal what the allowed values and its keys and labels are. Views will use that when displaying he field and everything will just work. If you define your field in Code, instead of hardcoding the list in there, you can also specify a ...


5

There are no ctools plugins anymore in Drupal 8. There's just the Drupal 8 plugin API. Page Manager, Panels and so on use standard block plugins now in 8.x, so you basically have to port this to a block an then it should work fine. Also, the "code" you posted only defines that you have such a plugin in your module, nothing else. If you need more specific ...


5

To answer the general question how do you do that with plugins: Each plugin type is managed by a plugin manager. You can list all plugin manager classes with this console command: drupal debug:plugin Most plugin managers provide an alter hook to change the plugin definitions. Look in the plugin manager class method __construct for a line with $this->...


4

Do it properly in a file (the plugin initialisation), add the file via a library, then load the library in the template file using twig: {{ attach_library('mytheme/mylibraryname') }} See https://www.drupal.org/developing/api/8/assets


4

The manual process is described on that plugin page itself, as you can see on the instruction page in first image(red circle.) If you click that link which you have provided in your question, you can see the Download option on that page. If you click it will download and it will open up a new pop-up window. 1. As you can see in the image. Copy that file ...


4

You need to get the current request object first: $request = \Drupal::request(); $range = $request->get('range'); Or $range = \Drupal::request()->get('range');


4

Copied from BlockContent::postSave(): // Invalidate the block cache to update custom block-based derivatives. \Drupal::service('plugin.manager.block')->clearCachedDefinitions();


4

Use a formattable markup: use Drupal\Component\Render\FormattableMarkup; $value = new FormattableMarkup('<i class="fa fa-mail"></i>@text', [ '@text' => $value, ]); The formattable string will not be escaped. The variable in the placeholder @text will be escaped and that is important, because it contains user input.


4

As per the suggestion by @googletorp, you should use hook_views_plugins_query_alter(). You simply update the plugin class with your own that overrides it. function my_mod_views_plugins_query_alter(array &$plugins) { $plugins['search_api_query']['class'] = 'Drupal\my_mod\Plugin\views\query\MymodSearchApiQuery'; }


4

Most of the time it is because the file is not picked up, there are a few common cases for that: Ensure that the filename matches the class name exactly, case sensitive. Without the extension of course. Ensure that the namespace matches the folder path exactly, case sensitive. Restart Apache to clear apcu cache, I've had issues with this when renaming/...


4

How old is the site exactly before you started updating it? Does it have the ctools module enabled? Someone at some point used/defined an entity_type:block_content_type condition, and ctools use to define those, but it was renamed a long time ago from entity_type:block_content_type to e.g. entity_bundle:block_content. By me, actually. If your site is that ...


4

With help from "Ambidex" on Drupal Slack, I post here the complete plugin-file. I could not find an example for this use case, and I believe it might benefit others: file: [drupalroot]/modules/custom/ourcustomername_tweaks/src/Plugin/Block /ContactBlock.php . <?php namespace Drupal\ourcustomername_tweaks\Plugin\Block; use Drupal\Core\Block\...


4

I am not sure that I quite get your question but in Drupal 8 things like nodes, files and users are entities. They all inherit from ContentEntityBase. Plugins are a way to implement/extend Drupal functionality such as Views filters and Field widgets. From the docs: Entity API Drupal 7 - entities were generic stdClass objects. Drupal 8 - entities ...


4

The documentation for t() states: When possible, use the \Drupal\Core\StringTranslation\StringTranslationTrait::t(). Otherwise create a new \Drupal\Core\StringTranslation\TranslatableMarkup object directly. It doesn't explain the reason for this, but if you look at the comment on the documentation page, it will give you a reason for using that trait ...


4

It looks like Drupal is trying to cache a form with an attached UploadedFile object in it. Is your custom Plugin a Block with a file element in it? In your blockForm function try: $form_state->disableCache() UPDATE 1 Based on @Aaron's feedback I updated buildForm to blockForm above. Note: Disabling caching for a form does have performance ...


4

You should inject the plugin manager as a service if possible, but for a plugin (code) based action: $manager = \Drupal::service('plugin.manager.action'); $action = $manager->createInstance('action_plugin_id') $action->execute(); Or if it's an advanced action, it's a config entity, so load and execute it that way: $action = \Drupal\system\Entity\...


3

What I would really like to be able to do is keep my existing block class (in whatever form) and pass that same single API call to 3 seperate templates that can be placed as three separate blocks. How do I do this? This is possible. Add a configuration option to the block class in blockForm() and then use this option in build() to change the content ...


Only top voted, non community-wiki answers of a minimum length are eligible