Skip to content

Instantly share code, notes, and snippets.

@gheydon
Created November 7, 2013 00:30
Show Gist options
  • Save gheydon/7346847 to your computer and use it in GitHub Desktop.
Save gheydon/7346847 to your computer and use it in GitHub Desktop.
Drupal 8 Param Converters (Drupal 7 Auto Loaders)
<?php
/**
* Implements hook_menu().
*/
function autoloader_example_d7_menu() {
$items = array();
$items['example/%autoloader_example_d7'] = array(
'title' => t('Autoload example data'),
'access callback' => 1,
'page callback' => 'autoloader_example_d7_page',
'page arguments' => array(1),
);
return $items;
}
/**
* load example object
*/
function autoloader_example_d7_load($id) {
return db_query('SELECT * FROM {autoloader_example_d7} WHERE id = :id', array(':id' => $id))->fetch();
}
/**
* display page
*/
function autoloader_example_d7_page($data) {
return t('Example object %id %title loaded', array('%id' => $data->id, '%title' => $data->title));
}
autoloader_example_d8.example_view:
path: '/example/{autoloader_example_d8}'
defaults:
_content: '\Drupal\autoloader_example_d8\Controller\ExampleController::page'
_title_callback: '\Drupal\autoloader_example_d8\Controller\ExampleController::pageTitle'
requirements:
_access: 1
options:
parameters:
autoloader_example_d8:
type: autoloader_example_d8
loader: TRUE
services:
paramconverter.autoloader_example_d8:
class: Drupal\autoloader_example_d8\ParamConverter\ExampleConverter
arguments: ['@entity.manager']
tags:
- { name: paramconverter, priority: 10 }
<?php
/**
* @file
* Contains \Drupal\autoloader_example_d8\Controller\ExampleController.
*/
namespace Drupal\autoloader_example_d8\Controller;
use Drupal\Component\Utility\String;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityInterface;
use Symfony\Component\HttpFoundation\Response;
/**
* Returns responses for Transaction routes.
*/
class ExampleController extends ControllerBase {
/**
* @param Drupal\mcapi\TransactionInterface $transaction
* The Transaction that we are displaying.
*
* @return array
* An array suitable for drupal_render().
*/
public function page($autoloader_example_d8) {
return t('Example object %id %title loaded', array('%id' => $autoloader_example_d8->id, '%title' => $autoloader_example_d8->title));
}
/**
* The _title_callback for the node.view route.
*
* @param NodeInterface $transaction
* The current node.
*
* @return string
* The page title.
*/
public function pageTitle($autoloader_example_d8) {
return String::checkPlain($autoloader_example_d8->label());
}
}
<?php
/**
* @file
* Contains \Drupal\autoloader_example_d8\ParamConverter\ExampleConverter.
*/
namespace Drupal\autoloader_example_d8\ParamConverter;
use Drupal\Core\Entity\EntityManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;
use Drupal\Core\ParamConverter\ParamConverterInterface;
/**
* Provides upcasting for a view entity to be used in the Views UI.
*
* Example:
*
* pattern: '/example/{autoloader_example_d8}'
* options:
* parameters:
* autoloader_example_d8:
* loader: TRUE
*/
class ExampleConverter extends ParamConverter implements ParamConverterInterface {
/**
* {@inheritdoc}
*/
public function convert($value, $definition, $name, array $defaults, Request $request) {
$entity_type = substr($definition['type'], strlen('entity:'));
if ($storage = $this->entityManager->getStorageController($entity_type)) {
$entity = $storage->loadByProperties(array('id' => $value, 'parent' => '0'));
if (!empty($entity)) {
return reset($entity);
}
}
}
/**
* {@inheritdoc}
*/
public function applies($definition, $name, Route $route) {
if (parent::applies($definition, $name, $route)) {
return !empty($definition['loader']) && $definition['type'] === 'entity:autoloader_example_d8';
}
return FALSE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment