Skip to content

Commit

Permalink
Merge pull request #226 from jmolivas/Issue-2358493
Browse files Browse the repository at this point in the history
Patch Issue 2358493 - Integration with Views for entity content generated
  • Loading branch information
jmolivas committed Oct 21, 2014
2 parents b132d00 + 4b9c228 commit 44e5a74
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Generator/EntityContentGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ public function generate($module, $entity_name, $entity_class)
$parameters
);

$this->renderFile(
'module/src/Entity/entity-content-views-data.php.twig',
$this->getEntityPath($module).'/'.$entity_class.'ViewsData.php',
$parameters
);

$this->renderFile(
'module/src/Entity/Controller/listcontroller-entity-content.php.twig',
$this->getEntityPath($module).'/Controller/'.$entity_class.'ListController.php',
Expand All @@ -93,5 +99,25 @@ public function generate($module, $entity_name, $entity_class)
$this->getEntityPath($module).'/Form/'.$entity_class.'DeleteForm.php',
$parameters
);

$this->renderFile(
'module/entity-content-page.php.twig',
$this->getModulePath($module).'/'.$entity_name.'.page.inc',
$parameters
);

$this->renderFile(
'module/templates/entity-html.twig',
$this->getTemplatePath($module).'/'.$entity_name.'.html.twig',
$parameters
);

$content = $this->renderView(
'module/src/Entity/entity-content.theme.php.twig',
$parameters
);

echo 'Add this to your hook_theme:' . PHP_EOL;
echo $content;
}
}
9 changes: 9 additions & 0 deletions src/Generator/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ protected function renderFile($template, $target, $parameters, $flag=null)
return file_put_contents($target, $this->render($template, $parameters), $flag);
}

protected function renderView($template,$parameters) {
return $this->render($template, $parameters);
}

public function getModulePath($module_name)
{
if (!$this->module_path) {
Expand Down Expand Up @@ -101,6 +105,11 @@ public function getEntityPath($module_name)
return $this->getModulePath($module_name).'/src/Entity';
}

public function getTemplatePath($module_name)
{
return $this->getModulePath($module_name).'/templates';
}

public function getServicesAsParameters()
{
$servicesAsParameters = new \Twig_SimpleFunction('servicesAsParameters', function ($services) {
Expand Down
1 change: 1 addition & 0 deletions src/Resources/skeleton/base/file.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/**
* @file
* Contains {% block file_path %}{% endblock %}
{% block extra_info %}{% endblock %}
*/
{% endblock %}
Expand Down
36 changes: 36 additions & 0 deletions src/Resources/skeleton/module/entity-content-page.php.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{% extends "base/file.php.twig" %}

{% block file_path %}
{{ entity_name }}.page.inc
{% endblock %}

{% block extra_info %}
* {{ entity_class }} page callback file for the {{ entity_name }} entity.
{% endblock %}

{% block use_class %}
use Drupal\Core\Render\Element;
{% endblock %}

{% block file_methods %}
/**
* Prepares variables for {{ entity_name }} templates.
*
* Default template: {{ entity_name }}.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the user information and any
* - attributes: HTML attributes for the containing element.
*/

function template_preprocess_{{ entity_name }}(&$variables) {
// Fetch {{ entity_class }} Entity Object
${{ entity_name }} = $variables['elements']['#{{ entity_name }}'];

// Helpful $content variable for templates.
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
}
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{% extends "base/class.php.twig" %}

{% block file_path %}
Drupal\{{ module }}\Entity\{{ entity_class }}.
{% endblock %}

{% block namespace_class %}
namespace Drupal\{{ module }}\Entity;
{% endblock %}

{% block use_class %}
use Drupal\views\EntityViewsData;
use Drupal\views\EntityViewsDataInterface;
{% endblock %}

{% block class_declaration %}
/**
* Provides the views data for the {{ entity_class }} entity type.
*/
class {{ entity_class }}ViewsData extends EntityViewsData implements EntityViewsDataInterface
{% endblock %}

{% block class_methods %}
/**
* {@inheritdoc}
*/
public function getViewsData() {
$data = parent::getViewsData();

$data['{{ entity_name }}']['table']['base'] = array(
'field' => 'id',
'title' => t('{{ entity_class }}'),
'help' => t('The {{ entity_name }} entity ID.'),
);

return $data;
}


{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use Drupal\user\UserInterface;
* handlers = {
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\{{ module }}\Entity\Controller\{{ entity_class }}ListController",
* "views_data" = "Drupal\{{ module }}\Entity\{{ entity_class }}ViewsData",
*
* "form" = {
* "add" = "Drupal\{{ module }}\Entity\Form\{{ entity_class }}Form",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% block hook_theme %}
$theme['{{ entity_name }}'] = array(
'render element' => 'elements',
'file' => '{{ entity_name }}.page.inc',
'template' => '{{ entity_name }}',
);
{% endblock %}
22 changes: 22 additions & 0 deletions src/Resources/skeleton/module/templates/entity-html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{{ '{#' }}
/**
* @file {{ entity_name }}.html.twig
* Default theme implementation to present {{ entity_class }} data.
*
* This template is used when viewing a {{ entity_name }} entity's page,
*
*
* Available variables:
* - content: A list of content items. Use 'content' to print all content, or
* - attributes: HTML attributes for the container element.
*
* @see template_preprocess_{{ entity_name }}()
*
* @ingroup themeable
*/
{{ '#}' }}
<div{{ '{{' }} attributes.addClass('{{ entity_name }}') {{ '}}' }}>
{{ '{%' }} if content {{ '%}' }}
{{ '{{' }}- content -{{ '}}' }}
{{ '{%' }} endif {{ '%}' }}
</div>

0 comments on commit 44e5a74

Please sign in to comment.