sgostanyan / sg_entity_services
Entity Services module.
Requires
- php: >=7.0
- drupal/core: >=8.7
This package is auto-updated.
Last update: 2025-04-23 17:20:01 UTC
README
SG Entity Services is a Drupal service for dealing with Drupal entities.
SgEntityStorageManager
### getEntities(string $entityType, array $bundles = NULL, array $ids = NULL) :
Return entities from given parameters.
$entityType: entity type id
$bundles: desired entity bundles (optional)
$ids: filtering for given ids (optional)
Usage:
Drupal::service('sg_entity_services.service')->getEntityStorageManager()->getEntities('node', ['article', 'event']);
createEntity(string $entityType, array $values, array $translations = NULL) :
Creates an entity with translations (optional)
$entityType: entity type id
$values: fields array
$translations: fields for translations (optional)
Usage:
$fieldValues = [
'title' => 'Title',
'body' => [
'value' => 'summary text',
],
'field_tags' => [
[
'target_id' => 1,
],
],
];
$translationsValues = [
'en' => [
'title' => 'EN title',
'body' => [
'value' => 'English summary',
],
'field_tags' => [
[
'target_id' => 1,
],
],
],
'es' => [
'title' => 'ES title',
'body' => [
'value' => 'Spanish summary',
],
'field_tags' => [
[
'target_id' => 1,
],
],
],
];
Drupal::service('sg_entity_services.service')->getEntityStorageManager()->createEntity('node', $fieldValues, $translationsValues);
### addTranslations(EntityInterface $entity, array $translations) :
Add translations for an existing entity.
$entity: entity interface
$translations: fields for translations
Usage:
$translationsValues = [
'en' => [
'title' => 'EN title',
'body' => [
'value' => 'English summary',
],
'field_tags' => [
[
'target_id' => 1,
],
],
],
'es' => [
'title' => 'ES title',
'body' => [
'value' => 'Spanish summary',
],
'field_tags' => [
[
'target_id' => 1,
],
],
],
];
$entity = Drupal::entityTypeManager()->getStorage('node')->load(46);
$entity = Drupal::service('sg_entity_services.service')->getEntityStorageManager()->addTranslations($entity, translationsValues);
SgEntityDisplayManager
### getViewModes(string $entityType) :
Return a list of available view modes for a given entity type.
$entityType: entity type
Usage:
$viewModes = Drupal::service('sg_entity_services.service')->getEntityDisplayManager()->getViewModes('node');
### renderEntity(EntityInterface $entity, $viewMode = NULL) :
Return a render array for a given entity.
$entity: entity interface
$viewMode: view mode
Usage:
$renderArray = Drupal::service('sg_entity_services.service')->getEntityDisplayManager()->renderEntity($entity, 'teaser'));
### renderArrayToMarkup(array $renderArray) :
Return a Markup object for a given render array.
$renderArray: render array
Usage:
$renderArray = Drupal::service('sg_entity_services.service')->getEntityDisplayManager()->renderEntity($entity, 'teaser'));
$markup = Drupal::service('sg_entity_services.service')->getEntityDisplayManager()->renderArrayToMarkup($renderArray);
htmlTagRender($tag, $value, array $attributes = []) :
Return a render array for a given tag element.
$tag: html tag type
$value: html tag's value
$attributes: array of html attributes
Usage:
$tagRender = Drupal::service('sg_entity_services.service')->getEntityDisplayManager()->htmlTagRender('a', 'click here', ['href' => 'https://drupal.org']);
imageStyleRender($fid, $imageStyle, $attributes = []) :
Return a render array for a given image id and style.
$fid: image file id
$imageStyle: image style id
$attributes: array of html attributes (optional)
Usage:
$imageRender = Drupal::service('sg_entity_services.service')->imageStyleRender(298, 'thumbnail', ['class' => ['thumb-style']]);
SgFileManager
### generateFileEntity($source, $filename, $destination = 'public://') :
Create a file entity and returns the file id.
$source: source folder (end slash is required)
$filename: name of file
$destination: file destination, can be 'public://' or 'private://' (default is public://). It can include subfolders, end slash is required
Usage:
$fid = Drupal::service('sg_entity_services.service')->getFileManager()->generateFileEntity('public://sources/', 'tiger.jpg', 'private://animals/');
### getFileInfos($fid) :
Return an array of file details: mime type, size, absolute url, relative url, internal url, system path, file usage.
$fid: file id
Usage:
$fileInfos = Drupal::service('sg_entity_services.service')->getFileManager()->getFileInfos(298);
### sanitizeFileSize($size) :
Return a formated file size.
$size: file size in bytes
Usage:
$fileInfos = Drupal::service('sg_entity_services.service')->getFileManager()->sanitizeFileSize(286567);
// $fileInfos = "287 Ko"
SgImageManager
### getImageStyles() :
Return a list of available image styles.
Usage:
$imageStyles = Drupal::service('sg_entity_services.service')->getImageManager()->getImageStyles();
getImageStyleUrl($fid, $imageStyle) :
Return styled image url.
$fid: file id
$imageStyle: image style id
Usage:
$imageUrl = Drupal::service('sg_entity_services.service')->getImageManager()->getImageStyleUrl(298, 'thumbnail');