How to check that module installed in Drupal 9?
if (\Drupal::service('module_handler')->moduleExists('locale')) {
// your code here
}
if (\Drupal::service('module_handler')->moduleExists('locale')) {
// your code here
}
Let's imagine we don't want to have any restrictions for our route. For this we can set _access requirement to TRUE.
your_module.route_name:
path: '/your_path'
defaults:
_controller: 'Drupal\your_module\Controller\YourController::yourMethod'
requirements:
_access: 'TRUE'
\Drupal::currentUser()->hasRole('administrator')
\Drupal::currentUser()->hasPermission('permission name');
Let's start from the most simple example. Fetch all nodes of type article
$query = \Drupal::service('entity_type.manager')->getStorage('node')->getQuery()
->condition('type', 'article')
$results = $query->execute();
Let's imagine our node have a simple field field_text and we want to fetch all nodes with some value.
$query = \Drupal::service('entity_type.manager')->getStorage('node')->getQuery()
->condition('field_text', 'my value')
$results = $query->execute();
Let's imagine we have a field_link of type Link. Such fields contain uri and title.
$query = \Drupal::service('entity_type.manager')->getStorage('node')->getQuery()
->condition('field_link', 'my value')
$results = $query->execute();
In this case by default uri will be used and we will get
WHERE node__field_link.field_link_uri = '1'
To filter by the title of the link we need to use the next code
$results = \Drupal::service('entity_type.manager')->getStorage('node')->getQuery()
->condition('field_link.title', 'my value')
$results = $query->execute();
Let's imagine we have field_tags which has a reference to taxonomy tags.
$query = \Drupal::service('entity_type.manager')->getStorage('node')->getQuery()
->condition('type', 'article')
->condition('field_tag.entity:taxonomy_term.name', 'My tag')
$results = $query->execute();
As a result we will get such SQL query:
SELECT base_table.vid AS vid, base_table.nid AS nid FROM node base_table INNER JOIN node__field_tag node__field_tag ON node__field_tag.entity_id = base_table.nid LEFT OUTER JOIN taxonomy_term_data taxonomy_term_data ON taxonomy_term_data.tid = node__field_tag.field_tag_target_id INNER JOIN taxonomy_term_field_data taxonomy_term_field_data ON taxonomy_term_field_data.tid = taxonomy_term_data.tid WHERE taxonomy_term_field_data.name LIKE 'My tag' ESCAPE '\\'SELECT base_table.vid AS vid, base_table.nid AS nid FROM node base_table INNER JOIN node__field_tag node__field_tag ON node__field_tag.entity_id = base_table.nid LEFT OUTER JOIN taxonomy_term_data taxonomy_term_data ON taxonomy_term_data.tid = node__field_tag.field_tag_target_id INNER JOIN taxonomy_term_field_data taxonomy_term_field_data ON taxonomy_term_field_data.tid = taxonomy_term_data.tid WHERE taxonomy_term_field_data.name LIKE 'My tag' ESCAPE '\\'SELECT base_table.vid AS vid, base_table.nid AS nid FROM node base_table INNER JOIN node__field_tag node__field_tag ON node__field_tag.entity_id = base_table.nid LEFT OUTER JOIN taxonomy_term_data taxonomy_term_data ON taxonomy_term_data.tid = node__field_tag.field_tag_target_id INNER JOIN taxonomy_term_field_data taxonomy_term_field_data ON taxonomy_term_field_data.tid = taxonomy_term_data.tid WHERE taxonomy_term_field_data.name LIKE 'My tag' ESCAPE '\\'
We can the same way extract the field of referenced entities and their referenced entities.
When we use autocomplete field, the submitted value will look something like this "Title of the entity (10)". To extract the current entity id from this string we can use helper function extractEntityIdFromAutocompleteInput from EntityAutocomplete
$autocomplete_data = 'Title of the entity (10)';
$entity_id = EntityAutocomplete::extractEntityIdFromAutocompleteInput($autocomplete_data);
To solve this issue we can use a small, but a very useful module Route condition. This module provides condition plugin, which will display the block based on routes. Basically it works the same way as Pages condition plugin. So, I believe you will not find any issues configure it.
Let's imagine we have a node with field category and we want to create a link in the view and pass a category in the request ?field_category=1. Unfortunately, it will not like this by default. Fortunately, we have a solution! We need a module prepopulated and pass a parameter a special way:
1) For entity reference fields
http://yourdoamain.com/node/add/page?edit[field_category][widget][0][target_id]=1
2) Normal fields
http://yourdoamain.com/node/add/page?edit[field_body][widget][0][value]=Something
If you have a basic path we can get route using router.no_access_checks service. Keep in my you have to have a leading slash
$router = \Drupal::service('router.no_access_checks');
$result = $router->match('/node/2');
In case you have URL object we can get route from it like this
$router = \Drupal::service('router.no_access_checks');
$result = $router->match($url->toString());
To get route by name in Drupal 9 we need to use router.route_provider service.
$route_provider = \Drupal::service('router.route_provider');
$route = $route_provider->getRouteByName('user.page');