Sorry The above path had a typo
lipinponmala007 → created an issue.
@LupusGr3y
What I mean to do is how a non entity router output can be transformed. in above, I am trying to get all the terms based on vocabulary_machine_name need as transformed array. This could be applied to if you are exposing any custom data as well suing the simple transform in the transfrom(). this works but i thought there is some other method which i could not figure it out yet.
I made a try on the custom route creation . It worked but thought like there could be a better way
But for those who are not getting anywhere in exposing your data in a custom non entity route
/** Example for creating a taxonomy list reponse **/
So tried below
modules/custom/ot_transform/ot_transform.routing.yml
ot_transform.vocabularyListPage:
path: 'vocabulary-list'
defaults:
_title: 'Vocabulary list'
_controller: '\Drupal\ot_transform\Controller\OtTransformVocabularyListPage::content'
requirements:
_permission: 'access content'
modules/custom/ot_transform/src/Controller/OtTransformVocabularyListPage.php
namespace Drupal\ot_transform\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Request;
/**
* Returns responses for OT Transform routes.
*/
final class OtTransformVocabularyListPage extends ControllerBase {
/**
* Controller content callback.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The request.
*
* @return array|\Symfony\Component\HttpFoundation\JsonResponse
* The render array or JSON response.
*/
public function content(Request $request) {
// Your normal content without ?format=json.
$content = [
'#markup' => $this->t('This is vocabulary page!'),
];
// Return the normal content.
return $content;
}
}
namespace Drupal\ot_transform\Plugin\Transform\Route;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Component\Plugin\DerivativeInspectionInterface;
use Drupal\transform_api\RouteTransformInterface;
use Drupal\transform_api\Transform\TransformInterface;
use Drupal\transform_api\Transform\EntityTransform;
/**
* Provides a description of your custom route transform plugin.
*
* @RouteTransform(
* id = "ot_transform.vocabularyListPage",
* title = @Translation("OT transform Vocabulary List"),
* )
*/
class OtTransformVocabularyListPage implements RouteTransformInterface, PluginInspectionInterface, DerivativeInspectionInterface {
/**
* Transform a transform of this type into JSON.
*
* @param \Drupal\transform_api\TransformInterface $transform
* The transform to transform.
*
* @return array
* Transformed JSON array.
*/
public function transform(TransformInterface $transform): array {
$terms = [];
$name = \Drupal::request()->get('vocabulary');
if ($name) {
/** @var \Drupal\taxonomy\Entity\Term[] $terms */
$termList = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['vid' => $name]);
foreach ($termList as $term) {
$terms[] = $term->id();
}
}
$transformation = [
"type" => "entity",
"field_section" => [
'field_section_type' => 'field_vocabulary',
"field_vocabulary" => new EntityTransform('taxonomy_term', $terms, 'default')
]
];
return $transformation;
}
/**
* {@inheritdoc}
*/
public function getPluginId() {
return $this->pluginId;
}
/**
* {@inheritdoc}
*/
public function getPluginDefinition() {
return [];
}
/**
* {@inheritdoc}
*/
public function getBaseId() {
return $this->pluginDefinition['base_id'];
}
/**
* {@inheritdoc}
*/
public function getDerivativeId() {
return $this->pluginDefinition['derivative_id'];
}
}