-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactoring Loaders & Renders #3
Merged
Merged
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ceaa1eb
Massive refactoring Loader & Render -> Finalize Loader, Next step Ren…
WengerK e99f3cf
Added bamboo_load_field
WengerK b39902b
Added bamboo_load_render
WengerK c818cf6
Remove disrupt_tools dependency
WengerK beb7ec3
Fixed Docblock
WengerK 53edcb9
Fixed render form
WengerK 730530b
Switch & of renderField
WengerK c266318
Use ->get() instead of ->{}
WengerK 2fb5d0c
Added FSO lazy loading
WengerK ac32502
Fix tests Loader Image
WengerK 7c7f8fe
Fix Tests create file bad file extension used
WengerK fa830a8
Added bamboo twig special image_style
WengerK 0c10f3b
Fix phpcs warnings
WengerK File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
name: Bamboo Twig - Loaders | ||
description: 'Several "Loaders & Render" Twig extensions.' | ||
package: Bamboo Twig | ||
type: module | ||
core: 8.x | ||
|
||
dependencies: | ||
- drupal:disrupt_tools |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
services: | ||
# Custom Twig extensions | ||
bamboo_twig_loader.twig.loader: | ||
class: Drupal\bamboo_twig_loader\TwigExtension\Loader | ||
tags: | ||
- { name: twig.extension } | ||
parent: bamboo_twig.twig.base | ||
bamboo_twig_loader.twig.render: | ||
class: Drupal\bamboo_twig_loader\TwigExtension\Render | ||
tags: | ||
- { name: twig.extension } | ||
parent: bamboo_twig.twig.base |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
<?php | ||
|
||
namespace Drupal\bamboo_twig_loader\TwigExtension; | ||
|
||
use Drupal\bamboo_twig\TwigExtension\TwigExtensionBase; | ||
|
||
/** | ||
* Provides some loaders as Twig Extensions. | ||
*/ | ||
class Loader extends TwigExtensionBase { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getFunctions() { | ||
return [ | ||
new \Twig_SimpleFunction('bamboo_load_entity', [$this, 'loadEntity']), | ||
new \Twig_SimpleFunction('bamboo_load_field', [$this, 'loadField']), | ||
new \Twig_SimpleFunction('bamboo_load_currentuser', [$this, 'loadCurrentUser']), | ||
new \Twig_SimpleFunction('bamboo_load_image', [$this, 'loadImage']), | ||
]; | ||
} | ||
|
||
/** | ||
* Unique identifier for this Twig extension. | ||
*/ | ||
public function getName() { | ||
return 'bamboo_twig_loader.twig.loader'; | ||
} | ||
|
||
/** | ||
* Returns an entity object. | ||
* | ||
* @param string $entity_type | ||
* The entity type. | ||
* @param mixed $id | ||
* (optional) The ID of the entity to load. | ||
* | ||
* @return null|Drupal\Core\Entity\EntityInterface | ||
* An entity object for the entity or NULL if the entity does not exist. | ||
*/ | ||
public function loadEntity($entity_type, $id = NULL) { | ||
$entity = $id ? | ||
$this->getEntityTypeManager()->getStorage($entity_type)->load($id) : | ||
$this->getCurrentRouteMatch()->getParameter($entity_type); | ||
|
||
if (!$entity) { | ||
return NULL; | ||
} | ||
|
||
return $entity; | ||
} | ||
|
||
/** | ||
* Returns the field object for a single entity field. | ||
* | ||
* @param string $field_name | ||
* The field name. | ||
* @param string $entity_type | ||
* The entity type. | ||
* @param mixed $id | ||
* (optional) The ID of the entity to render. | ||
* @param string $langcode | ||
* (optional) Language code to load translation. | ||
* | ||
* @return null|Drupal\Core\Field\FieldItemListInterface | ||
* A field object for the entity or NULL if the value does not exist. | ||
*/ | ||
public function loadField($field_name, $entity_type, $id = NULL, $langcode = NULL) { | ||
$entity = $this->loadEntity($entity_type, $id); | ||
|
||
if ($entity && $langcode && $entity->hasTranslation($langcode)) { | ||
$entity = $entity->getTranslation($langcode); | ||
} | ||
|
||
// Ensure the entity has the requested field. | ||
if (!$entity->hasField($field_name)) { | ||
return NULL; | ||
} | ||
|
||
// Do not continue if the field is empty. | ||
if ($entity->get($field_name)->isEmpty()) { | ||
return NULL; | ||
} | ||
|
||
if (isset($entity->{$field_name})) { | ||
return $entity->{$field_name}; | ||
} | ||
|
||
return NULL; | ||
} | ||
|
||
/** | ||
* Return the current user object. | ||
* | ||
* @return \Drupal\user\Entity\User|null | ||
* The current user object or NULL when anonymous. | ||
*/ | ||
public function loadCurrentUser() { | ||
$currentUser = $this->getCurrentUser(); | ||
|
||
if ($currentUser->isAnonymous()) { | ||
return NULL; | ||
} | ||
|
||
return $this->getUserStorage()->load($currentUser->id()); | ||
} | ||
|
||
/** | ||
* Load a ImageInterface object for an original image path or URI. | ||
* | ||
* @param string $path | ||
* The path or URI to the original image. | ||
* | ||
* @return \Drupal\Core\Image\ImageInterface | ||
* An Image object. | ||
*/ | ||
public function loadImage($path) { | ||
return $this->getImageFactory()->get($path); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add
bamboo_load_menu
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Create a issue for this here : #7
People will chose.