Hot answers tagged

29

There is the loadByProperties method in the \Drupal\Core\Entity\EntityStorageInterface that allows to define required condition in searching the file entity by the given file URI: /** @var \Drupal\file\FileInterface[] $files */ $files = \Drupal::entityTypeManager() ->getStorage('file') ->loadByProperties(['uri' => $uri]); /** @var \Drupal\file\...


15

Update Official advice found at: Use issue forks to make compatibility fixes work with Composer Copied here for convenience: It's impossible to install an incompatible module with composer and apply a compatibility patch afterwards. However, since issue forks are branches it's possible to install the module using that branch. In order to do this we need to ...


12

All the properties can be searched and found from the code in Drupal core. And they seems to be the properties of drupalSettings object in JS. This object is initialised in core/misc/drupalSettingsLoader.js. (function () { 'use strict'; // Use direct child elements to harden against XSS exploits when CSP is on. var settingsElement = document....


11

create-project can take a version number, and recommended-project has some tags in for 9.x, so for example: composer create-project drupal/recommended-project mynewproject "9.0-beta1" Or if you just want the bleeding edge to play with: composer create-project drupal/recommended-project mynewproject "^9.0" --stability=dev --prefer-dist


10

You should use Drush. Drush is about to be added to the drupal/recommended-project, see #3159647, while Drupal Console seems to be abandoned and currently is incompatible with Drupal 9 due to mismatched dependencies, see #4251. Maybe Drupal Console finally accomplished its mission by triggering the modernisation of Drush. And there's no need for two CLIs. ...


8

In your Entity Reference Field settings the Default method will allow all of the entities of the type you specified to be eligible to be referenced. If you're setting up your ER field to reference Taxonomy terms of a certain vocabulary then all of the terms from that Vocabulary will be able to be chosen inside that field. If you want more control over which ...


7

To make a theme suggestion work in the module folder you need an additional theme hook: function offer_theme($existing, $type, $theme, $path) { return [ 'offer' => [ 'render element' => 'elements', ], 'offer__full' => [ 'base hook' => 'offer', ], ]; }


6

As folks (Hudri, leymannx) alluded to in the comments, entity.manager was deprecated in Drupal 8 which means it was removed in Drupal 9. The project that has the code that's calling entity.manager is not ready for Drupal 9 (or at least the version you have installed is not ready for Drupal 9). You may want to look for alternative modules if the one your ...


6

Yes, upgrading from Drupal 8 to 9 is scheduled to be an incremental update from the last minor version of Drupal 8. It seems the main thing to consider is to remove any usage of deprecated functions to ensure a smooth upgrade. Contributed project developers, as well as those maintaining custom integrations, should follow the deprecations when possible ...


6

For a wider explanation of why Darvanen's answer is necessary, see https://www.computerminds.co.uk/articles/apply-drupal-9-compatibility-patches-composer (disclaimer: I wrote this). But do note that using a different vendor namespace (like drupal_git) shouldn't be necessary if the section is added to your composer.json file's 'repositories' above the one for ...


6

At first glance Settings appears to be read-only, however the constructor is quite forgiving in its parameters. We can construct a fake settings structure like so: $site_settings = [ 'http_services_api' => [ 'auth_services' => [ 'title' => 'Auth Services', 'config' => [ 'base_uri' => 'https://demo.api-platform.com/...


6

Yes, it is possible, with the attributes property. my-library: version: 1.x js: js/App.js: { attributes: { type: module } } See Adding attributes to script elements and JS properties.


6

I post this as an answer, because someone might be as stupid as I am :) I browsed various old backups and found the settings.php line that grants admin permissions to anonymous user, and it worked. If you add: $config['user.role.anonymous']['is_admin'] = 'true'; in your settings.php, every anonymous user is admin. That way I could get back into admin panel ...


6

WebP is supported in the GD image toolkit. As noted in the change record, you can check whether your version of PHP has WebP support by going to admin/reports/status/php#module_gd. What if I am using Imagemagick? If you do a lot of image processing on your site, you may be using the ImageMagick toolkit via the ImageMagick contrib module. In this case, it ...


6

Unless something is overriding the routing, the route for all nodes is entity.node.canonical. The route needs a route_parameter, node, to pass the nid. For example, you could create a link to a node's page with Link::createFromRoute() like: $my_link = Link::createFromRoute('My link text', 'entity.node.canonical', [ 'node' => 123, ]); As @4k4 pointed ...


6

Entity queries don't support expressions. To get it around it, add a tag to the query: $query->addTag('mymodule'); Then implement hook_query_alter(), check for the tag, and add the expression there instead: function mymodule_query_alter(Drupal\Core\Database\Query\AlterableInterface $query) { if ($query instanceof Drupal\Core\Database\Query\...


5

The variable html_attributes is not available in a page template. To make it available copy this code from template_preprocess_html() to your page preprocess hook: use Drupal\Core\Template\Attribute; /** * Implements hook_preprocess_page(). */ function mytheme_preprocess_page(&$variables) { $variables['html_attributes'] = new Attribute(); $...


5

Seems like Drush which came with the setup was not fully compatible. I installed it again using the composer using composer require drush/drush It is working as expected. Here is updated "require" section looks like in composer.json "require": { "composer/installers": "^1.9", "drupal/...


5

You can alter a webform by modifying the YAML content of the config field elements: use Drupal\webform\Entity\Webform; use Drupal\Core\Serialization\Yaml; $webform = Webform::load('contact'); $elements = Yaml::decode($webform->get('elements')); // add a field $elements['additional_field'] = [ '#type' => 'textfield', '#title' => '...


5

To properly manage fields added by a module, so that a field is added and removed when the module is installed and uninstalled, add an enforced dependency for your own module to the yaml file: /config/install/field.storage.node.field_example.yml ... dependencies: enforced: module: - mymodule See https://www.drupal.org/node/2404447


5

The 401 error is because you're not sending the basic auth creds like you are with your curl request. You need to add an Authorization header to your Guzzle request: 'Authorization' => 'Basic ' . base64_encode($user . ':' . $pass), The 500 error is likely due to a malformed payload that JSONAPI can't parse. The value of json should be an associative array,...


5

Seems the following class is responsible for determining a separate field storage table: \Drupal\Core\Entity\Sql\DefaultTableMapping::allowsSharedTableStorage It returns a boolean based on the following 4 conditions: public function allowsSharedTableStorage(FieldStorageDefinitionInterface $storage_definition) { return !$storage_definition->...


5

The raw filter is still fine to use. The filter is not deprecated, only the raw tag. From the Drupal 9 theming documentation: As of Twig 1.x, the raw tag is deprecated in favor of verbatim. Looking at the Twig 2.x documentation, you'll notice that the raw filter is not deprecated. However, in the Twig 1.x documentation for the verbatim tag you can see that ...


5

Since Devel 4 you have to add Kint youself: composer require drupal/devel kint-php/kint You can read through this epic issue to learn more: Unknown "kint" function. in Twig\ExpressionParser. Also important to read the release notes. On all 4.0.0+ releases the following is written. You have to do this before upgrading: If you have Kint module ...


5

The 403 error page is not a redirect, so there is no referer. It's rendered in a sub-request which by default is using the route system.403. From inside of the sub-request you can get the route name of the original request with \Drupal::routeMatch()->getMasterRouteMatch()->getRouteName()


5

addMessage() accepts a string or a MarkupInterface as its argument. If you intend to pass HTML to addMessage(), you need to construct a render array for that markup then explicitly render it. For example: $build = [ '#type' => 'container', '#markup' => t('Successfully submitted'), '#attributes' => ['class' => ['custom-messages--status']], ];...


4

As the EntityManager docs say: Deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager() instead in most cases. If the needed method is not on \Drupal\Core\Entity\EntityTypeManagerInterface, see the deprecated \Drupal\Core\Entity\EntityManager to find the correct interface or service. $...


4

I have a use case where the user needs to be able to copy the date from another source and paste that value into the date field. Bootstrap Datepicker This will allow you to type/paste in and change the date format. Note: composer won't install the library for you, you have to manually get the /dist folder from github and put in in the /libraries folder. So ...


4

As discussed here, Hide path on frontpage redirect, you need to make caching of route collections domain-specific. Other than the discussed overriding of the core service, since Drupal 8.8 you can use the service method addExtraCacheKeyPart(). Add this code in a request event subscriber: /src/EventSubscriber/RouteCacheDomainSubscriber.php <?php namespace ...


4

Seems like the version of devel_generate you're using isn't fully compatible with PHP 8.0. This issue fixed the problem: https://gitlab.com/drupalspoons/devel/-/issues/356 Replacing drupal/devel:4.0.1 in the docker file with the dev version containing the patch (drupal/devel:4.x-dev#c8d48e2dab1d92a74fd17b5a91d005476e91b9b0) resolves the issue.


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