Browse Source

Code clean-up

merge-requests/2/head 3.0.0-rc1
Chi 4 years ago
parent
commit
62e3cdd7b7
  1. 2
      README.md
  2. 28
      src/TwigTweakExtension.php

2
README.md

@ -33,7 +33,7 @@ drush ev "print_r(array_keys(\Drupal::service('plugin.manager.block')->getDefini
{{ drupal_block('system_branding_block') }} {{ drupal_block('system_branding_block') }}
{# Print block using custom configuration. #} {# Print block using custom configuration. #}
{{ drupal_block('system_branding_block', {label: 'Branding', use_site_name: false}) }} {{ drupal_block('system_branding_block', {label: 'Branding', use_site_name: false, id}) }}
{# Bypass block.html.twig theming. #} {# Bypass block.html.twig theming. #}
{{ drupal_block('system_branding_block', wrapper=false) }} {{ drupal_block('system_branding_block', wrapper=false) }}

28
src/TwigTweakExtension.php

@ -144,15 +144,15 @@ class TwigTweakExtension extends AbstractExtension {
/** /**
* Builds the render array of a given region. * Builds the render array of a given region.
*/ */
public static function drupalRegion(string $region, string $theme = NULL): array { public static function drupalRegion(string $region, ?string $theme = NULL): array {
return \Drupal::service('twig_tweak.region_view_builder')->build($region, $theme); return \Drupal::service('twig_tweak.region_view_builder')->build($region, $theme);
} }
/** /**
* Returns the render array to represent an entity. * Returns the render array to represent an entity.
*/ */
public static function drupalEntity(string $entity_type, ?string $id, string $view_mode = 'full', string $langcode = NULL, bool $check_access = TRUE): array { public static function drupalEntity(string $entity_type, string $id, string $view_mode = 'full', ?string $langcode = NULL, bool $check_access = TRUE): array {
$entity = \Drupal::entityTypeManager()->getStorage($entity_type)->load((string) $id); $entity = \Drupal::entityTypeManager()->getStorage($entity_type)->load($id);
if ($entity) { if ($entity) {
return \Drupal::service('twig_tweak.entity_view_builder') return \Drupal::service('twig_tweak.entity_view_builder')
->build($entity, $view_mode, $langcode, $check_access); ->build($entity, $view_mode, $langcode, $check_access);
@ -163,7 +163,7 @@ class TwigTweakExtension extends AbstractExtension {
/** /**
* Gets the built and processed entity form for the given entity type. * Gets the built and processed entity form for the given entity type.
*/ */
public static function drupalEntityForm(string $entity_type, string $id = NULL, string $form_mode = 'default', array $values = [], bool $check_access = TRUE): array { public static function drupalEntityForm(string $entity_type, ?string $id = NULL, string $form_mode = 'default', array $values = [], bool $check_access = TRUE): array {
$entity_storage = \Drupal::entityTypeManager()->getStorage($entity_type); $entity_storage = \Drupal::entityTypeManager()->getStorage($entity_type);
$entity = $id ? $entity_storage->load($id) : $entity_storage->create($values); $entity = $id ? $entity_storage->load($id) : $entity_storage->create($values);
if ($entity) { if ($entity) {
@ -176,8 +176,8 @@ class TwigTweakExtension extends AbstractExtension {
/** /**
* Returns the render array for a single entity field. * Returns the render array for a single entity field.
*/ */
public static function drupalField(string $field_name, ?string $entity_type, ?string $id, string $view_mode = 'full', string $langcode = NULL, bool $check_access = TRUE): array { public static function drupalField(string $field_name, string $entity_type, string $id, string $view_mode = 'full', string $langcode = NULL, bool $check_access = TRUE): array {
$entity = \Drupal::entityTypeManager()->getStorage($entity_type)->load((string) $id); $entity = \Drupal::entityTypeManager()->getStorage($entity_type)->load($id);
if ($entity) { if ($entity) {
return \Drupal::service('twig_tweak.field_view_builder') return \Drupal::service('twig_tweak.field_view_builder')
->build($entity, $field_name, $view_mode, $langcode, $check_access); ->build($entity, $field_name, $view_mode, $langcode, $check_access);
@ -211,22 +211,22 @@ class TwigTweakExtension extends AbstractExtension {
/** /**
* Builds an image. * Builds an image.
*/ */
public static function drupalImage(string $property, string $style = NULL, array $attributes = [], bool $responsive = FALSE, bool $check_access = TRUE): array { public static function drupalImage(string $selector, string $style = NULL, array $attributes = [], bool $responsive = FALSE, bool $check_access = TRUE): array {
// Determine property type by its value. // Determine selector type by its value.
if (preg_match('/^\d+$/', $property)) { if (preg_match('/^\d+$/', $selector)) {
$property_type = 'fid'; $selector_type = 'fid';
} }
elseif (Uuid::isValid($property)) { elseif (Uuid::isValid($selector)) {
$property_type = 'uuid'; $selector_type = 'uuid';
} }
else { else {
$property_type = 'uri'; $selector_type = 'uri';
} }
$files = \Drupal::entityTypeManager() $files = \Drupal::entityTypeManager()
->getStorage('file') ->getStorage('file')
->loadByProperties([$property_type => $property]); ->loadByProperties([$selector_type => $selector]);
// To avoid ambiguity render nothing unless exact one image has been found. // To avoid ambiguity render nothing unless exact one image has been found.
if (count($files) != 1) { if (count($files) != 1) {

Loading…
Cancel
Save