Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By vijayan08 on
Hi,
I want to create views image field plugin
Image is not render into views list here is my code
URL only render into view not thumbnail image
<?php
/**
* @file
* Definition of Drupal\globe_common\Plugin\views\field\MediaFrontendUrl
*/
namespace Drupal\globe_common\Plugin\views\field;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\globe_common\Services\MediaFileResponseService;
use Drupal\file\Entity\File;
use Drupal\media\Entity\Media;
use Drupal\Core\State\State;
use Drupal\image\Entity\ImageStyle;
/**
*
* @ingroup views_field_handlers
*
* @ViewsField("media_frontend_url")
*/
class MediaFrontendUrl extends FieldPluginBase implements ContainerFactoryPluginInterface {
/**
* @var MediaFileResponseService
*/
protected $mediaFileResponseService;
public function __construct(array $configuration, $plugin_id, $plugin_definition, MediaFileResponseService $mediaResponseService) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->mediaFileResponseService = $mediaResponseService;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('globe_common.media_response_array')
);
}
/**
* @{inheritdoc}
*/
public function query() {
// Leave empty to avoid a query on this field.
}
/**
* @{inheritdoc}
*/
public function render(ResultRow $values) {
$mediaEntity = $values->_entity;
//kint($mediaEntity);
//kint($mediaEntity);
//die;
$mediaId = $mediaEntity->get('mid')->value;
$angularfrontEndURL = \Drupal::state()->get('globe-online.front_end_site_url');
$media = Media::load($mediaId);
$fileId = $media->field_media_image->target_id;
//$imageDetails = $this->mediaFileResponseService->getRelativeFileURL($fileId);
// kint($imageDetails);
//$imageExplode = explode("/",$imageDetails);
//$getImageValue = end($imageExplode);
//kint(end($imageExplode));
//$array_reversed = array_reverse($imageExplode, true);
//$getImageValue = array_values($array_reversed);
//kint($getImageValue[0]);
//die;
/*$getFileName = File::load($fileId);
$fileName = $getFileName->getFilename();*/
$file = File::load($fileId);
$image_uri = ImageStyle::load('thumbnail')->buildUrl($file->getFileUri());
return $this->t($image_uri);
}
}
<?php
/**
* Implements hook_views_data_alter().
*/
function globe_common_views_data_alter(array &$data) {
$data['media']['media_frontend_url'] = array(
'title' => t('Media Front End Url'),
'field' => array(
'title' => t('Media Front End Url'),
'help' => t('Media Front End'),
'id' => 'media_frontend_url',
),
);
}
Comments
For what do you need this,
For what do you need this, what is the use case?
i need to generate my own custom thumbnail image in the view
i need to generate my own custom thumbnail image in the view, so i have try to add view plugin field, but image is not display
Why not just add the "Image"
Why not just add the "Image" field to the entity?