By podarok on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
dev-integra
Introduced in version:
dev-integra
Description:
In order to let Drupal core to proper caching on container level as well as handle transactions without race conditions, we should follow best practices and use Drupal API to pull data from the field, not a raw SQL query.
Drupal API also handles different DB backends properly, while raw SQL queries would have issues.
Before
$query = \Drupal::database()->select('node__field_category', 'n');
$query->leftjoin('taxonomy_term_field_data', 'tfd', 'tfd.tid = n.field_category_target_id');
$query->addExpression('GROUP_CONCAT(DISTINCT tfd.name)', 'cats');
$query->condition('n.entity_id', $nid);
$categories = $query->execute()->fetchField();
After
$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof NodeInterface) {
if (!empty($node->get('field_category'))) {
$categories = $node->get('field_category');
}
}
Impacts:
Module developers
Site templates, recipes and distribution developers