Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/opdavies/drupal-tips
A work-in-progress collection of tips for working with Drupal code.
https://github.com/opdavies/drupal-tips
drupal php
Last synced: about 2 months ago
JSON representation
A work-in-progress collection of tips for working with Drupal code.
- Host: GitHub
- URL: https://github.com/opdavies/drupal-tips
- Owner: opdavies
- Created: 2022-02-02T15:09:11.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-02-08T13:00:49.000Z (almost 3 years ago)
- Last Synced: 2024-12-18T01:09:46.326Z (about 2 months ago)
- Topics: drupal, php
- Homepage:
- Size: 2.93 KB
- Stars: 0
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
Awesome Lists containing this project
README
..
TODO:
- Cron jobs
- Custom plugins (e.g. blocks)
- Drush commands
- Forms
- JSON:API (filtering, field names etc)
- Logging
- Querying the database
- Queues and workersDrupal tips
###########A collection of tips and tricks for working with `Drupal `_ code. Ideas and PRs welcome.
Inspired by https://github.com/PovilasKorop/laravel-tips.
.. contents::
:depth: 2Entity storage
==============Loading a single entity by ID
-----------------------------.. code:: php
\Drupal::entityTypeManager()->getStorage('node')->load(1);
The entity type manager can also be loaded using the ``entity_type.manager`` service name. For example:
.. code:: php
\Drupal::service('entity_type.manager')->getStorage('node')->load(1);
Loading multiple entities by ID
-------------------------------.. code:: php
\Drupal::entityTypeManager()->getStorage('node')->loadMultiple([1, 2]);
Loading entities by properties
------------------------------.. code:: php
use Drupal\node\NodeInterface;
$nodeStorage = \Drupal::entityTypeManager()->getStorage('node');
// Load all published `event` nodes.
$nodeStorage->loadByProperties([
'status' => NodeInterface::PUBLISHED,
'type' => 'event',
]);// Load all published `talk` nodes.
$nodeStorage->loadByProperties([
'status' => NodeInterface::PUBLISHED,
'type' => 'talk',
]);Querying for entities
---------------------Returns an instance of ``Drupal\Core\Entity\Query\QueryInterface`` for the specified entity type.
.. code:: php
$nodeStorage = \Drupal::entityTypeManager()->getStorage('node');
// Load all nodes.
$nodeStorage->getQuery()->execute();// Load nodes that match the specified conditions.
$nodeStorage->getQuery()
->condition('type', 'event')
->condition('title', '%Online%', 'LIKE')
->range(0, 10)
->addTag('node_access')
->execute();Services
========Using a class name as a service name
------------------------------------Before:
.. code:: yaml
# my_module.services.yml
services:
my_module.example_service:
class: Drupal\my_module\Service\ExampleServiceAfter:
.. code:: yaml
# my_module.services.yml
services:
Drupal\my_module\Service\ExampleService: []Automatically inject dependencies with autowiring
-------------------------------------------------Before:
.. code:: yaml
# my_module.services.yml
services:
Drupal\my_module\Service\ExampleService:
arguments: ['@entity_type.manager']After:
.. code:: yaml
# my_module.services.yml
services:
Drupal\my_module\Service\ExampleService:
autowire: trueControllers
===========Controllers as services
-----------------------.. code-block:: yaml
# my_module.services.yml
services:
Drupal\my_module\Controller\ExampleController: []Single-action controllers
-------------------------Before:
.. code-block:: yaml
# my_module.routing.yml
my_module.example:
path: '/example'
defaults:
_controller: 'Drupal\my_module\Controller\ExampleController::handle'
requirements:
_permission: 'access content'.. code-block:: php
// modules/my_module/src/Controller/ExampleController.php
class ExampleController {
public function handle() {
// ...
}}
After:
.. code-block:: yaml
# my_module.routing.yml
my_module.example:
path: '/example'
defaults:
_controller: 'Drupal\my_module\Controller\ExampleController'
requirements:
_permission: 'access content'.. code-block:: php
// modules/my_module/src/Controller/ExampleController.php
class ExampleController {
public function __invoke() {
// ...
}}
Automated testing
=================* `Workshop notes `_
* `Workshop code `_