Skip to content
Snippets Groups Projects
Commit fc8c7391 authored by Timo Kirkkala's avatar Timo Kirkkala
Browse files

Initial commit.

parents
Branches
Tags
No related merge requests found
JSON:API image styles
=======
Adds image styles of a drupal image to jsonapi
name: 'JSON:API image styles'
type: module
description: 'Expose image styles to JSON:API'
core: 8.x
package: 'Web services'
dependencies:
- drupal:jsonapi
<?php
/**
* @file
* Contains jsonapi_image_styles.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
/**
* Implements hook_help().
*/
function jsonapi_image_styles_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the jsonapi_image_styles module.
case 'help.page.jsonapi_image_styles':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Expose image styles to JSON:API') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_entity_base_field_info().
*/
function jsonapi_image_styles_entity_base_field_info(EntityTypeInterface $entity_type) {
$fields = [];
$base_table = $entity_type->getBaseTable();
// Certain classes are just not supported.
$original_class = $entity_type->getOriginalClass();
if (!empty($base_table) && $original_class == 'Drupal\file\Entity\File') {
$fields['image_style_uri'] = BaseFieldDefinition::create('image_style_uri')
->setLabel(t('Entity image styles'))
->setDescription(t('Image styles of the file entity'))
->setComputed(TRUE)
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->setTranslatable(TRUE);
}
return $fields;
}
<?php
namespace Drupal\jsonapi_image_styles\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
/**
* Plugin implementation of the 'image_style_uri' field type.
*
* @FieldType(
* id = "image_style_uri",
* label = @Translation("Image style uri"),
* description = @Translation("Normalized image style paths"),
* no_ui = TRUE,
* list_class = "\Drupal\jsonapi_image_styles\Plugin\Field\FieldType\ImageStyleNormalizedFieldItemList",
* )
*/
class ImageStyleNormalizedFieldItem extends FieldItemBase {
/**
* {@inheritdoc}
*/
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [];
}
/**
* {@inheritdoc}
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['url'] = DataDefinition::create('any')
->setLabel(t('URI'))
->setRequired(TRUE);
return $properties;
}
/**
* {@inheritdoc}
*/
public function isEmpty() {
$value = $this->get('url')->getValue();
return $value === serialize([]);
}
}
<?php
namespace Drupal\jsonapi_image_styles\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemList;
use Drupal\Core\TypedData\ComputedItemListTrait;
use Drupal\file\Entity\File;
use Drupal\image\Entity\ImageStyle;
/**
* Represents the computed image styles for a file entity.
*/
class ImageStyleNormalizedFieldItemList extends FieldItemList {
use ComputedItemListTrait;
/**
* {@inheritdoc}
*/
protected function computeValue() {
$entity = $this->getEntity();
$uri = ($entity instanceof File) ? $entity->getFileUri() : FALSE;
if (!$entity->id() || !$uri) {
return;
}
$offset = 0;
// @TODO: Make it a configuration option to define image styles to expose.
$styles = [
'large',
'thumbnail',
];
foreach ($styles as $style) {
$image_style = ImageStyle::load($style);
$url = ($image_style instanceof ImageStyle) ? $image_style->buildUrl($uri) : $url = file_create_url($uri);
$this->list[] = $this->createItem(
$offset,
[
'url' => [$style => $url],
]
);
$offset++;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment