Hot answers tagged

15

First (but read on for the correct, better aproach) it's important to note that Drupal 9.2 broke the simple way of doing it: $response = new RedirectResponse($path); $response->send(); You have to use the full code from the drupal_goto() changelog, namely: $response = new RedirectResponse($url->toString()); $request = \Drupal::request(); // Save the ...


8

First, run Composer require cweagans/composer-patches Now you have a package installed that enables you to patch core, libraries or modules. Second, add a patch to a folder named 'patches' in the root (where your composer.json file is). Third, add an "extras" section in composer with a patch: "extra": { "patches": { ...


8

You can using like jQuery ajax. Because Drupal.ajax({}).execute() is a function and return the jqXHR. /** * Execute the ajax request. * * Allows developers to execute an Ajax request manually without specifying * an event to respond to. * * @return {object} * Returns the jQuery.Deferred object underlying the Ajax request. If * ...


7

First, it's probably preferable to use the provider option if you can. However, Field widgets and formatters also make use of an isApplicable() method. You can use this for any additional logic to determine whether your widget should be available for a field, a field in the subset of fields defined in your annotation. You can use this method to limit the ...


7

Yes, this is possible. You may declare a dependency on a module by using the "provider" key in the annotation. For your example, this would look like: /** * @FieldWidget( * id = "my_daterange", * label = @Translation("Date and time range + All day"), * field_types = { * "daterange" * }, * provider =...


6

Generally speaking, a view is used to show data for a list of entities. View filters are used to select (or filter) which entities are handled by the view; contextual filters are used to select the entities basing on the values passed in the URL. For example, the Files view that comes with Drupal 8 has two pages: one whose path is /admin/content/files and ...


6

I had to do this recently and found this solution using Drupal 9.1. $file = File::load($fid); $relative_file_url = $file->createFileUrl(TRUE); $absolute_file_url = $file->createFileUrl(FALSE); I found the createFileUrl() function in web/core/modules/file/src/Entity/File.php. Maybe this would be considered "the way?"


6

Then don't use custom block types because they are stored in content entities. Use blocks storing only configuration. You can build a custom block plugin, even with a body field. See Custom block build function to display wysiwyg body text There are also some contrib options like Simple Block which is implementing a config entity for blocks.


6

Provide a controller to download the PDF as discussed in the comments: https://stefvanlooveren.me/blog/serve-pdf-download-controller-drupal-8 <?php namespace Drupal\MYMODULE\Controller; use Drupal\Core\Controller\ControllerBase; use Symfony\Component\HttpFoundation\BinaryFileResponse; /** * Class PdfGeneratorController. */ class ...


6

Thanks to @leymannx composer update drupal/core drupal/core-dev --with-dependencies did it


6

You just seem to be getting confused with the form build concept and PHP logic - what you need to do is add to the existing form array ($form in buildForm), not create a second separate form/array. With that in mind, this: $this->secondForm($param); Doesn't do anything. You're not merging the return value back into $form, and you're not passing $form to ...


6

Create a custom content entity with entity_reference fields relating each "email" entity to a node and a user. The information you're saving is neither part of the node's content nor part of the node's metadata. It is metadata about an email that has been sent. Since the email records aren't related to any particular revision of the node, you want ...


6

Use the Mime Mail module The Mime Mail module gives Drupal core and other Drupal modules the ability to send HTML email messages. This is an all-Drupal solution that does not require external third-party libraries.


6

$term_name = \Drupal\taxonomy\Entity\Term::load($term_id)->get('name')->value; You will get the Term Name


5

Drush 9 drush cdel core.extension module.MYMODULE


5

A Drupal URI starts with a stream wrapper like "public://" that indicates to Drupal how to handle that resource. Typically, the "public" stream wrapper is defined as "treat this resource as a local file within sites/default/files." Your module assets are not located within sites/default/files, so they cannot use the "public&...


5

A taxonomy field is referencing entities, it has no value property. See how to print value of a taxonomy field to headline with node.html.twig? Converting this to a token pattern: [node:field_content_class:entity:name]/[node:title]


5

TableSort has methods to retrieve the sorting order and the field being sorted directly from the query arguments. You can use the values returned by ::getSort() and ::getOrder() to do a custom sorting of your data. In your render array you can use some (undocumented) options in the #header declaration to control the way the table is initially sorted: field: ...


5

Normally the original entity is stored in the entity object before the user input is applied. If not, you can get it via the loaded revision id, which is preserved when an entity is loaded to keep it save from changes: if (isset($entity->original)) { $original = $entity->original; } else { $original = \Drupal::entityTypeManager() ->...


5

You could add a listing cache tag for the content type: 'tags' => ['node_list:message'], This cache tag was introduced in Drupal 8.9. See the change record Added an ENTITY_TYPE_list:BUNDLE cache tag.


5

How does BigPipe have an effect if my traffic is mostly anonymous and behind varnish? This really depends on the content of the page. Taking away any content depending on the logged-in user there might be no dynamic part left which could have any effect. Additionally, why does the second request bypass Varnish? As mentioned in the module description it is ...


5

If you are using Paragraphs Browser Field Widgets, then the WIDGET_TYPE ids for hook hook_field_widget_WIDGET_TYPE_form_alter() are different: Field Widget Label Field Widget ID Provider Paragraphs Classic entity_reference_paragraphs Paragraphs Paragraphs EXPERIMENTAL paragraphs Paragraphs Paragraphs Browser Classic entity_reference_paragraphs_browser ...


5

In Views, if you go into the Settings (/admin/structure/views/settings), you can check the box Show the SQL query to see the magic. I made a very simple view of content. Here's the SQL query: SELECT node_field_data.created AS node_field_data_created, node_field_data.nid AS nid FROM {node_field_data} node_field_data WHERE node_field_data.status = '1' ORDER ...


5

Here is the code: {{ content.field_title|render|striptags|trim|lower }} it works fine.


5

You can go to the content types (or paragraph type) Form display settings and set the number of rows. No dev needed: e.g. /admin/structure/types/manage/content_type/form-display Click on the gear in the right column Click update Click save at the bottom


5

I just figured out that there is the 'Submit' element at the end of the webform elements under webform build tab and I can apply the conditions on that 'Submit' element.


5

Starting Drupal 8.9.0, there is a new ENTITY_TYPE_list:BUNDLE cache tag added (see the change record). So in your case you would simply need to tag our list with node_list:book. https://www.drupal.org/node/3107058


5

I haven't found a way to run this commands from Drupal 8 database connection object. I can only do selects, updates, inserts or deletes That's not true, you can also run arbitrary queries, e.g.: \Drupal::database()->query('TRUNCATE TABLE foo');


5

Configuration management Deploying content types, fields, views, site settings (everything that is not content) from one environment (local) to another (stage, prod etc.) in Drupal 8+ works by exporting the currently active configuration (stored in the database) into YAML files. The most common approach is to simply export all your site's configuration at ...


5

I’ve been using the log_stdout module for stdout logging. It works well with Docker containers. This module provides a hook for Drupal Watchdog that directs log messages to php://stdout or php://stderr for better log handling with Docker.


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