Drupal 8 custom block (module) create twig template file - php

I have a custom Module that creates a custom Block which has field elements.
This all works fine but I need to theme this block. I have checked the other posts on here and tried with no luck.
I have enabled twig debug and got theme suggestions. Still no luck.
Can anyone please point me in the right direction.
This is what I have so far:
my_module/my_module.module
// nothing related in here
my_module/src/Plugin/Block/myModuleBlock.php
<?php
namespace Drupal\my_module\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Provides a 'ModuleBlock' block.
*
* #Block(
* id = "module_block",
* admin_label = #Translation("My Module"),
* )
*/
class ModuleBlock extends BlockBase {
public function blockForm($form, FormStateInterface $form_state) {
$form['test'] = array(
'#type' => 'select',
'#title' => $this->t('test'),
'#description' => $this->t('test list'),
'#options' => array(
'Test' => $this->t('Test'),
),
'#default_value' => isset($this->configuration['test']) ? $this->configuration['test'] : 'Test',
'#size' => 0,
'#weight' => '10',
'#required' => TRUE,
);
return $form;
}
/**
* {#inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['test'] = $form_state->getValue('test');
}
/**
* {#inheritdoc}
*/
public function build() {
$build = [];
$build['module_block_test']['#markup'] = '<p>' . $this->configuration['test'] . '</p>';
return $build;
}
}
my_module/templates/block--my-module.html.twig // as suggested by twig debug
<h1>This is a test</h1>
<div id="test-widget">{{ content }}</div>
I should also note that in my my_theme.theme I have this but I don;t think its relevant:
// Add content type suggestions.
function my_theme_theme_suggestions_page_alter(array &$suggestions, array $variables) {
if ($node = \Drupal::request()->attributes->get('node')) {
array_splice($suggestions, 1, 0, 'page__node__' . $node->getType());
}
}
As for what I've tried is this:
public function build() {
return array(
'#theme' => 'block--my-module'
);
}
But still no go.
Any help here is very much appreciated.
UPDATE: So I just got it to work but I still need help. I moved the template block--my-module.html.twig to my theme directory and it worked.
How do I get it to work in my module directory?

UPDATE: So I just got it to work but I still need help. I moved the
template block--my-module.html.twig to my theme directory and it
worked.
How do I get it to work in my module directory?
You can create a directory called templates/ in your modules root.
Place your template here.
Now let Drupal know you store the template in your module.
in your_module.module add this function:
function YOUR_MODULE_theme($existing, $type, $theme, $path) {
return array(
'block__my_module' => array(
'render element' => 'elements',
'template' => 'block--my-module',
'base hook' => 'block'
)
);
}
This is not tested. It´s the way it worked for my custom block.
Don´t forget to clear the cache.

To be able to add the twig file in your module, you need to make sure the module defines the reference, not the theme.
You can still implement hook_theme() in the module's .module file as follows:
function mymodule_theme($existing, $type, $theme, $path) {
return [
'mymodule_block' => [
'variables' => [
// define defaults for any variables you want in the twig file
'attributes' => [
'class' => ['my-module-class'],
], //etc
],
],
];
}
Then in your block's build() implementation you can add a reference to the new theme function:
public function build() {
// Load the configuration from the form
$config = $this->getConfiguration();
$test_value = isset($config['test']) ? $config['test'] : '';
$build = [];
$build['#theme'] = 'mymodule_block';
// You would not do both of these things...
$build['#test_value'] = $test_value;
$build['module_block_test']['#markup'] = '<p>' . $test_value . '</p>';
return $build;
}
Finally be careful about where you place your twig file and what you name it. Create a templates directory in your module directory, and replace the _ in the theme function name with -: mymodule-block.html.twig

Related

How to change order of items in SilverStripe GridField

I use a GridField in SilverStripe to create HTML section items that are rendered on a page. This works so far but it always displays the sections in the order that I added them to the CMS or rather by the ID it gets when it's created.
So my question is: How can i change that order. I don't want to manually change the IDs but would rather do a simple drag and drop.
Edit: Could the use of Elemental be a solution to this problem?
Screenshot of the CMS view
The Page:
class HomePage extends Page
{
private static $has_many = [
'SectionObjects' => SectionObject::class
];
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Sections',
$grid = GridField::create('SectionObjects', 'Sections', $this->SectionObjects(), GridFieldConfig_RecordEditor::create())
);
$config = $grid->getConfig();
$dataColumns = $config->getComponentByType(GridFieldDataColumns::class);
$dataColumns->setDisplayFields([
'ID' => 'ID',
'LastEdited' => 'Changed'
]);
return $fields;
}
}
The Section object
class SectionObject extends DataObject{
private static $db = [
'Content' => 'Text',
'BgColor' => Color::class
];
private static $has_one = [
'HomePage' => HomePage::class
];
public function getCMSFields(){
return new FieldList(
TextareaField::create('Content','SectionContent'),
ColorField::create('BgColor', 'Hintergrundfarbe')
);
}
}
Definitely using the elemental module would allow you to have the kind of behaviour you want - but if you don't want to go to the effort of refactoring your code to comply with that module, you could use either the gridfieldextensions or sortablegridfield module to allow you to sort the gridfield (with drag and drop) to your heart's content.

How can I add a custom product "Sort by" field in prestashop?

I am new to Prestashop and I am trying to add a new "Sort by" field ( where by default you have: "Relevance" , "Name, A to Z" , "Name, Z to A", "Price, low to high", "Price, high to low" )
As you guys know, the functionality is located in the module called:
"Ps_facetedsearch" , link here.
I tried:
Editing the module files, this works, but I can't upgrade the module anymore if I want to keep the functionality.
Overriding, but can't seem to get it working, it still uses the same old module, not the overriden one.
So my questions are:
How can you add the additional "Sort by" field in the products listing (front) in a most elegant/easiest way possible?I would love to hear for any other approaches to this problem.
Can you do this without override/s, if you, for e.g, have bought another module that overrides the main module ( "Ps_facetedsearch", so that two overrides would not conflict)
Any tips are appreciated!!!
PrestaShop version: 1.7.4.2
The lines in the Ps_facetedsearch module that I need to copy/paste in order to add an additional "Sort by" field:
private function getAvailableSortOrders()
{
return [
(new SortOrder('product', 'position', 'asc'))->setLabel(
$this->module->getTranslator()->trans('Relevance', array(), 'Modules.Facetedsearch.Shop')
),
(new SortOrder('product', 'name', 'asc'))->setLabel(
$this->module->getTranslator()->trans('Name, A to Z', array(), 'Shop.Theme.Catalog')
),
(new SortOrder('product', 'name', 'desc'))->setLabel(
$this->module->getTranslator()->trans('Name, Z to A', array(), 'Shop.Theme.Catalog')
),
(new SortOrder('product', 'price', 'asc'))->setLabel(
$this->module->getTranslator()->trans('Price, low to high', array(), 'Shop.Theme.Catalog')
),
(new SortOrder('product', 'price', 'desc'))->setLabel(
$this->module->getTranslator()->trans('Price, high to low', array(), 'Shop.Theme.Catalog')
)
// copy and paste here for another one, but lose the upgradability
// of a module.
];
}
Found in Ps_FacetedsearchProductSearchProvider.php
(lines 117-136)
You can add custom sort by option by overriding Ps_Facetedsearch module.
You can follow below steps to add custom sort by order.
1) Add file ps_facetedsearch.php in folder override/modules/ps_facetedsearch; (create folders if not exists) and below code in this file.
<?php
/**
* #override Ps_Facetedsearch
*/
if (!defined('_PS_VERSION_')) {
exit;
}
require_once implode(DIRECTORY_SEPARATOR, array(
__DIR__, 'src', 'Ps_FacetedsearchProductSearchProvider.php',
));
class Ps_FacetedsearchOverride extends Ps_Facetedsearch
{
public function hookProductSearchProvider($params)
{
$query = $params['query'];
// do something with query,
// e.g. use $query->getIdCategory()
// to choose a template for filters.
// Query is an instance of:
// PrestaShop\PrestaShop\Core\Product\Search\ProductSearchQuery
if ($query->getIdCategory()) {
return new Ps_FacetedsearchProductSearchProviderOverride($this);
} else {
return null;
}
}
}
2) Add file Ps_FacetedsearchProductSearchProvider.php in folder override/modules/ps_facetedsearch/src; (create folders if not exists) and add below code in it.
<?php
require_once implode(DIRECTORY_SEPARATOR, array(
__DIR__, '..', '..', '..', '..', 'modules', 'ps_facetedsearch', 'src', 'Ps_FacetedsearchProductSearchProvider.php',
));
require_once implode(DIRECTORY_SEPARATOR, array(
__DIR__, '..', '..', '..', '..', 'modules', 'ps_facetedsearch', 'src', 'Ps_FacetedsearchFiltersConverter.php',
));
require_once implode(DIRECTORY_SEPARATOR, array(
__DIR__, '..', '..', '..', '..', 'modules', 'ps_facetedsearch', 'src', 'Ps_FacetedsearchFacetsURLSerializer.php',
));
use PrestaShop\PrestaShop\Core\Product\Search\URLFragmentSerializer;
use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchProviderInterface;
use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchContext;
use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchQuery;
use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchResult;
use PrestaShop\PrestaShop\Core\Product\Search\Facet;
use PrestaShop\PrestaShop\Core\Product\Search\FacetCollection;
use PrestaShop\PrestaShop\Core\Product\Search\Filter;
use PrestaShop\PrestaShop\Core\Product\Search\SortOrder;
class Ps_FacetedsearchProductSearchProviderOverride extends Ps_FacetedsearchProductSearchProvider
{
private $module;
public function __construct(Ps_Facetedsearch $module)
{
$this->module = $module;
}
public function runQuery(
ProductSearchContext $context,
ProductSearchQuery $query
) {
$facetedSearch = new Ps_FacetedsearchProductSearchProvider($this->module);
$result = $facetedSearch->runQuery($context, $query);
$sortOrders = $this->getAvailableSortOrders();
foreach ($sortOrders as $sortOrder) {
$result->addAvailableSortOrder($sortOrder);
}
return $result;
}
/**
* New sort order that needs to be appended
*
* #return array
*/
private function getAvailableSortOrders()
{
return [
// add your custom sort by orders here;
];
}
}
3) Make sure overrides is enabled in backend; from Advance Parameters > Performance
4) To load you overrides you need to re-index autoloads and to do so you need to delete class_index.php file; delete class_index.php file from var/cache/dev and var/cache/prod folders.
5) Check you shop; new custom sort order will be added.
Hope it helps!
For 1.7.4.x
Go to /modules/ps_facetedsearch/src
Open Ps_FacetedsearchProductSearchProvider.php
Find private function getAvailableSortOrders() line
and add what you want like:
(new SortOrder('product', 'date_add', 'desc'))->setLabel(
$this->module->getTranslator()->trans('Date added: latest first', array(), 'Modules.Facetedsearch.Shop')
),
(new SortOrder('product', 'date_add', 'asc'))->setLabel(
$this->module->getTranslator()->trans('Date added: earliest first', array(), 'Modules.Facetedsearch.Shop')
),
(new SortOrder('product', 'position', 'asc'))->setLabel(
$this->module->getTranslator()->trans('Relevance', array(), 'Modules.Facetedsearch.Shop')
),
for 1.7.6.x
You can find the file in /modules/ps_facetedsearch/src/Product/SearchProvider.php
and find:
private function getAvailableSortOrders()
for old versions:
- themes/default/product-sort.tpl
- add like:
<option value="date_add:desc" {if $orderby eq 'date_add' AND $orderway eq 'desc'}selected="selected"{/if}>{l s='Date added: latest first'}</option>
Since ps_facetedsearch module gone through some changes lately here is version for latest PS 1.7.7.1 release, based on #Divjesh answer:
Add file ps_facetedsearch.php in folder override/modules/ps_facetedsearch (create folders if not exists) and put below code into this file.
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
require_once implode(DIRECTORY_SEPARATOR, array(
__DIR__, 'src', 'Product', 'SearchProvider.php',
));
use PrestaShop\PrestaShop\Core\Product\Search\SortOrder;
use PrestaShop\Module\FacetedSearch\URLSerializer;
use PrestaShop\Module\FacetedSearch\Filters\Converter;
class Ps_FacetedsearchOverride extends Ps_Facetedsearch
{
public function hookProductSearchProvider($params)
{
$query = $params['query'];
if ($query->getIdCategory()) {
$urlSerializer = new URLSerializer();
return new SearchProviderOverride(
$this,
new Converter(
$this->getContext(),
$this->getDatabase(),
$urlSerializer
),
$urlSerializer
);
} else {
return null;
}
}
}
Add file SearchProvider.php in folder override/modules/ps_facetedsearch/src/Product/ (create folders if not exists) and add put below code in it:
<?php
use PrestaShop\Module\FacetedSearch\Filters;
use PrestaShop\Module\FacetedSearch\URLSerializer;
use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchContext;
use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchQuery;
use PrestaShop\PrestaShop\Core\Product\Search\SortOrder;
use PrestaShop\Module\FacetedSearch\Product\SearchProvider;
class SearchProviderOverride extends SearchProvider
{
private $_module;
public function __construct(
Ps_Facetedsearch $module,
Filters\Converter $converter,
URLSerializer $serializer,
SearchFactory $searchFactory = null
) {
parent::__construct($module, $converter, $serializer, $searchFactory);
$this->_module = $module;
}
public function runQuery(
ProductSearchContext $context,
ProductSearchQuery $query
) {
$result = parent::runQuery($context, $query);
$result->setAvailableSortOrders($this->getMyAvailableSortOrders());
return $result;
}
private function getMyAvailableSortOrders()
{
// define your SortOrders here for example:
$sortDateDesc = new SortOrder('product', 'date_add', 'desc');
$translator = $this->_module->getTranslator();
return [
$sortDateDesc->setLabel(
$translator->trans('Newest', [], 'Modules.Facetedsearch.Shop')
),
];
}
}

Is there a way for a block service to get the page ID from which it is being called?

In my Symfony 3.3 application, I have built a block service using SonataBlockBundle. Now I want to pull some other field values from the page on which the block lives. In other words, I want to do something like this:
public function configureSettings(OptionsResolver $resolver)
{
$pageRepository = $this->doctrine->getRepository('ApplicationSonataPageBundle:Page');
$pageId = someMagicalMethodCall();
$page = $repository->findOneBy(['id' => $pageId]);
$images = $page->getImageUrls;
$resolver->setDefaults(array(
'content' => 'Some custom content',
'images' => $images,
'template' => 'AppBundle:Block:block_media.html.twig',
));
}
Is this possible? If so, what would I put in place of someMagicalMethodCall in the block above?
It is possible, but you need to inject additional service in your block - CmsManagerSelector. Then in your configureSettings you need to retrieve the proper manager and get the current page instance from it. For example in your code:
public function configureSettings(OptionsResolver $resolver)
{
$cmsManager = $this->cmsManagerSelector->retrieve();
// $page will be the Page object already, no need to call doctrine repository. Reference: https://github.com/sonata-project/SonataPageBundle/blob/3.x/src/CmsManager/BaseCmsPageManager.php#L38
$page = $cmsManager->getCurrentPage();
$images = $page->getImageUrls;
$resolver->setDefaults(array(
'content' => 'Some custom content',
'images' => $images,
'template' => 'AppBundle:Block:block_media.html.twig',
));
}
Thanks to Jakub Krawczyk and a mentor, I found this page:
Getting instance of container in custom sonata block
... which led me to another way of getting the page related to a block, from within the execute() method. So I now have the following code, which serves me well:
public function execute(BlockContextInterface $blockContext, Response $response = null)
{
$page = $blockContext->getBlock()->getPage();
$localImages = $page->getImages();
$imageProvider = $this->provider;
foreach ($localImages as $key => $image) {
$publicImages[$key]['url'] = $imageProvider->generatePublicUrl($image, 'reference');
$publicImages[$key]['name'] = $image->getName();
}
$settings = $blockContext->getSettings();
$settings['images'] = $publicImages;
return $this->renderResponse($blockContext->getTemplate(), array(
'block' => $blockContext->getBlock(),
'settings' => $settings,
), $response);
}
Again, thanks to all involved.

CakePHP 3 - call object from other helper

I'm actually working on a Helper for CakePHP3 that include BsHelper and then the BsFormHelper.
Actually everything looks good, no problem with Bootstrap formats.
I try now to create a ckEditor instance, but I meet some several problems.
If i try to call my ckEditor like this :
$this->BsForm->ckEditor('test')
I just have some problems because the function ckEditor is in my BsFormHelper, and load function is in BsHelper. So when i try to access private var to know if i had to load ckEditor i got that issue :
Error: Call to a member function load() on a non-object
File C:\wamp3\www\wac_lucien\BsHelpersCakePHP3\3.2\plugins\BsHelpers\src\View\Helper\BsFormHelper.php
So in fact I know where is the issue :
In BsFormHelper my fonction looks like :
public function ckEditor($fieldName, $options = array(), $ckEditorOptions = array()) {
$options['type'] = 'textarea';
$out = $this->input($fieldName, $options);
// If there is a point in the fieldName
if (strpos($fieldName, '.') !== false) {
$nameForReplace = Inflector::camelize(Inflector::slug($fieldName));
} else {
$nameForReplace = $this->_modelForm . Inflector::camelize($fieldName);
}
$this->Bs->load('ckeditor');
$this->Bs->loadJS('CKEDITOR.replace("' . $nameForReplace . '", ' . json_encode($ckEditorOptions) . ');', true);
return $out;
}
And in my BsHelper i got :
public function load($key) {
if (!$this->__extensions[$key]['loaded']) {
foreach ($this->__extensions[$key]['css'] as $css) {
$this->loadCSS($css);
}
foreach ($this->__extensions[$key]['js'] as $js) {
$this->loadJS($js);
}
$this->__extensions[$key]['loaded'] = true;
}
return $this->__extensions[$key]['loaded'];
}
Values are in declaration like this
public $__extensions = array(
'jasny' => array(
'css' => array(
'//cdnjs.cloudflare.com/ajax/libs/jasny-bootstrap/3.1.3/css/jasny-bootstrap.min.css'
),
'js' => array(
'//cdnjs.cloudflare.com/ajax/libs/jasny-bootstrap/3.1.3/js/jasny-bootstrap.min.js'
),
'loaded' => true
),
'ckeditor' => array(
'css' => array(),
'js' => array(
'//cdn.ckeditor.com/4.5.8/standard/ckeditor.js'
),
'loaded' => true
)
);
Can someone help me to find out ? It looks like load function called in BsFormHelper can't access privates vars from BsHelper ...
seems you are just trying to use a helper in another helper
The manual says
You may wish to use some functionality already existing in another
helper. To do so, you can specify helpers you wish to use with a
$helpers array, formatted just as you would in a controller:
So in your BsFormHelper just do
public $helpers = ['Bs'];
and you're done

Drupal custom modules and cache issue

We created a few custom custom modules for a Drupal site and we have the following issue : each time we create or update a content (whatever it is), the content generated by the custom modules disappears. We have to clear all caches to get this content to appear again.
As it is our first experience with Drupal, we are certainly missing something but we don't know what.
Any help would be greatly appreciated!
Below is the code of one of these custom modules:
File website_actualites.module
<?php
/**
* Implements hook_block_info().
*/
function website_actualites_block_info() {
$blocks['website_actualites'] = array(
'info' => t('website_actualites'),
'cache' => DRUPAL_CACHE_PER_ROLE,
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function website_actualites_block_view($delta = '') {
$aDelta = explode('+', $delta);
$nbActualite = 2;
if (!empty($aDelta[1])) {
$nbActualite = $aDelta[1];
}
$block = null;
switch ($aDelta[0]) {
case 'website_actualites':
$block['content'] = _website_actualites_sweet_block_content($nbActualite);
break;
}
return $block;
}
/**
* Callback implemented by hook_block_view().
*/
function _website_actualites_sweet_block_content($nbActualite=2) {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'article')
->propertyCondition('status', 1)
->fieldCondition('field_mise_en_avant', 'value', 1)
->propertyOrderBy('created', 'DESC')
->range(0, $nbActualite)
->addMetaData('account', user_load(1));
$result = $query->execute();
// width col pour nb actu=2 : 4,8
// width col pour nb actu=4: 2,4,2,4
$colWidthEven=4;
$colWidthOdd=8;
if (4 == $nbActualite) {
$colWidthEven=2;
$colWidthOdd=4;
}
$data = array();
if (isset($result['node'])) {
$nids = array_keys($result['node']);
$items = entity_load('node', $nids);
$i=0;
foreach ($items as $item) {
$colWidth=$colWidthOdd;
if (0 == $i%2) {
$colWidth = $colWidthEven;
}
$i++;
$data[$item->nid] = array(
'title' => $item->title,
'tags' => isset($item->fielsTags['und'][0]) ? $item->fielsTags['und'][0] : '',
'body' => isset($item->body['und'][0]['value']) ? $item->body['und'][0]['value'] : '',
'image' => isset($item->field_image['und'][0]) ? $item->field_image['und'][0] : '',
'nid' => $item->nid,
'col-width' => $colWidth,
'alias' => drupal_get_path_alias('node/'.$item->nid)
);
}
}
$static_title = t('Static Title');
$static_content = 'static content';
return theme('website_actualites_output', array(
'title' => $static_title,
'content' => $static_content,
'data' => $data
)
);
}
File website_actualite-sweet--block.tpl.php
<?php
foreach ($data as &$row) {
$url = drupal_get_path_alias('node/' . $row['nid']);
$imageWrapper = file_stream_wrapper_get_instance_by_uri($row['image']['uri']);
if (is_object($imageWrapper) && is_callable(array($imageWrapper, 'getExternalUrl'))) {
$imageUrl = $imageWrapper->getExternalUrl();
print '<div class="col-sm-'.$row['col-width'].'">
<div class="img">
<img src="' . $imageUrl . '" class="img-responsive" alt="image description">
</div>
<p>' . $row['title'] . '</p>
</div>';
}
}
first of all there are several problems with this code. If you're going to use Drupal then you need to stick to Drupal coding standards. Avoid using camel casing for variables, instead you should use underscores. Also you should use 2 spaces for indentation.
In your block info function you define a single block however you assign it to an unassigned variable:
// Define $blocks first.
$blocks = array();
$blocks['website_actualites'] = array(
Next issue is that you call hook_block_view($delta), this call will be fired for every block delta, and regardless of the block delta you are setting it to null (also for future reference should be NULL):
$block = null;
So Drupal is building the render array for each block, and you are wiping all the data. You are doing this because you are trying to use the block delta as a way to pass a parameter into your block with a delta like website_actualites+10. Blocks aren't designed to work this way and delta's are meant to be static so that Drupal can keep track of them in the database and perform the correct caching according to your cache flag. You've defined the delta website_actualites in your hook_block_info() but Drupal has no knowledge or configuration information for any other block delta.
If you have a need for the same block to display varying quantities, then just define several blocks (in your hook_block_info()) and call the same helper function as you are now. If there is a requirement to configure the block then you should use the hook_block_configure() API
Now the data you are passing into your theme function, you are extracting field data from the node object using: $item->field_image['und'][0]. A couple of things here: fields are provided by the field module which has an extensive API to retrieve field data. You should use field_get_items() instead, which will return an array of all items associated with that field for that entity (as fields can be multi-valued) and handles the language for you. The other thing is never use 'und' use the constant LANGUAGE_NONE.
$body = field_get_items('node', $item, 'body');
$field_image = field_get_items('node', $item, 'field_image');
$field_tags = field_get_items('node', $item, 'field_tags');
$data[$item->nid] = array(
'title' => $item->title,
'tags' => $field_tags ? $field_tags : FALSE,
'body' => $body ? $body[0] : FALSE,
'image' => $field_image ? $field_image[0] : FALSE,
'nid' => $item->nid,
'col-width' => $colWidth,
'alias' => drupal_get_path_alias('node/'.$item->nid)
);
Another problem is that you set the $block['content'] to the result of theme('website_actualites_output', ...). Now this function will return markup, Drupal has many more calls in the chain before we need to render any markup, and the problem with calling this now is that you can't mutate your data at any point now in the build process.
In order to call theme('website_actualites_output', ...); You need to have defined this theme function in a hook_theme() call, presumably you have done this in another module, which is perfectly fine but remember to add a dependency on that module in your module.info file. What you really want to be doing is just adding instructions to the Drupal build, not markup:
return array(
'#theme' => 'website_actualites_output',
'#title' => $static_title,
'#content' => $static_content,
'#data' => $data
);
So then we come to your template, you have a lot of logic in your template and they are only intended for displaying content with markup, with ideally no, or minimal computation if any. A theme function has a hook_preprocess() call before hook_process(), and then the variables are passed into the template.
You are using this theme function to loop over the $data variable to display markup - ideally a theme function would be just that markup with the variables already processed and passed into it:
website-actualites-output--child.tpl.php
<div class="col-sm-<?php print $col_width; ?>">
<?php if ($image): ?>
<div class="img">
<?php print render($image); ?>
</div>
<?php endif; ?>
<?php if ($title): ?>
<p><?php print render($title); ?></p>
<?php endif; ?>
</div>
In this example your best bet would be to have a second theme function website_actualites_output__child with the above template.
/**
* Implements hook_theme().
*/
function website_actualites_theme() {
$templates = drupal_get_path('module', 'website_actualites') . '/templates';
return array(
'website_actualites_output__child' => array(
'path' => $templates,
'template' => 'website-actualites-output--child',
'variables' => array(
'col_width' => 4, // Default col width.
'image' => NULL,
'title' => NULL,
),
),
);
}
Then preprocess your current theme function:
/**
* Implements hook_preprocess_HOOK().
*/
function website_actualites_preprocess_website_actualites_output($variables) {
$processed_output = array();
$data = $variables['data'];
foreach ($data as $row) {
$image = array(
'#theme' => 'image',
'#path' => file_create_url($row['image']['uri']),
'#alt' => $image['alt'],
'#attributes' => array('class' => array('img-responsive')),
);
$processed_output[] = array(
'#theme' => 'website_actualites_output__child',
'#col_width' => $row['col-width'],
'#image' => array(
'#theme' => 'link',
'#path' => 'node/' . $row['nid'],
'#text' => $image,
'#options' => array('HTML' => TRUE),
),
'#title' => array(
'#theme' => 'link',
'#path' => 'node/' . $row['nid'],
'#text' => $row['title'],
),
);
}
// Reassign the $data variable so that we can just render() it.
// var_dump($processed_output) to get a better idea what's going on here.
$variables['data'] = $processed_output;
}
Then all you need to do in your current template is to:
<?php if ($data): ?>
<?php print render($data); ?>
<?php endif; ?>
And you can wrap this is whatever markup you like.
The Drupal learning curve is high, but remember that if someone else were to maintain this code after you, they would expect that you adhere to Drupal coding standards and followed the process upon which Drupal is built.
Further reading: Render Arrays in Drupal 7
Hope this information helps and makes it a bit clearer.

Categories