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.
Related
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.
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
I'm trying to pass one variable from different product pages to a webform page. Each product page has a session variable like $_SESSION["product_name"] = "Product1";. Similarly, other product pages have $_SESSION["product_name"] = "Product2"; $_SESSION["product_name"] = "Product3";
My question is how to pass these variables to a different webform page. I'm using "webform" module, what I want to accomplish is there should be a <select> tag, and there are options like
<select>
<option value="<?php echo $_SESSION["product_name"]; ?>" selected><? php echo $_SESSION["product_name"]; ?></option>
<option value="Product2">Product2</option>
<option value="Product3">Product3</option>
<option value="Product4">Product4</option>
</select>
The problem is there is no place for me to place these codes, is there any way around this or is there any module I can use for this issue?
You can create a custom module and then implement hook_form__alter()
You would need to find the IDs of the webforms you want to target.
Then:
function mymodule_form_webform1_alter(&$form, &$form_state) {
array_unshift($form['products']['#options'], array($_SESSION["product_name"] => $_SESSION["product_name"]));
}
or similar
This might be a way to pass the session value to webform option default value. I'm using Drupal 7, Webform 4. The session values on different product pages are:
$_SESSION["j_product_name"] = "haha1";
$_SESSION["j_product_name"] = "haha2";
First I created a custom module, the .module part is(also need to create .info):
<?php
function jay_tokens_token_info() {
$types['jay_token'] = array(
'name' => t("Jay Tokens"),
'description' => t("Jay custom tokens"),
);
$jay_token['product_session'] = array(
'name' => t("Product Name"),
'description' => t("product_name session value"),
);
return array(
'types' => $types,
'tokens' => array(
'jay_token' => $jay_token,
),
);
}
/**
* Implements hook_tokens().
*/
function jay_tokens_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
if ($type == 'jay_token') {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'product_session':
$replacements[$original] = $_SESSION["j_product_name"];
break;
}
}
}
return $replacements;
}
?>
In webform select options, the default value should be [jay_token:product_session] and options should be:
haha1|Product1
haha2|Product2
i've been using and studying Collin Williams template plugin (http://williamsconcepts.com/ci/codeigniter/libraries/template/reference.html#manipulation) and i've already posted this issue on CI's forum but i think the last post was last year maybe its not being monitored by Colllin or wat but i guess i'll just have to post this here maybe you guys can help.
Original Post on CI Forum
Hello Collin,
I’ve been studying your template plugin lately, as i was following your guide,
i came across this line of code
$data = array('name' => 'John Smith', 'birthdate' => '11/15/1950');
$this->template->write_view('content', 'user/profile', $data, TRUE);
it was a bit confusing whether in the view files, like
mymastertemplate.php for example, how do i accessthe $data array, does
it have to be $content defined by that first param. a region, or by
$name and $birthdate? ... cuz’ it says there $content will display the
data array? its a bit confusing. Hope you could enlighten me.
Basically thats my problem.
On Template.php library we can see function write_view(). Now, focus on $data = NULL. Now then finds a file of existed data on APPPATH.'views/'.$suggestion.'.php' so I think that $args[0] should be a file which is loaded and break it, than loaded a view template on $data.
function write_view($region, $view, $data = NULL, $overwrite = FALSE)
{
$args = func_get_args();
// Get rid of non-views
unset($args[0], $args[2], $args[3]);
// Do we have more view suggestions?
if (count($args) > 1)
{
foreach ($args as $suggestion)
{
if (file_exists(APPPATH .'views/'. $suggestion . EXT) or file_exists(APPPATH .'views/'. $suggestion))
{
// Just change the $view arg so the rest of our method works as normal
$view = $suggestion;
break;
}
}
}
$content = $this->CI->load->view($view, $data, TRUE);
$this->write($region, $content, $overwrite);
}
In another way, $data should be as array which will response for View template data on Codeigniter library (standard view of CI: $this->CI->load->view(...))
$data = array('name' => 'John Smith', 'birthdate' => '11/15/1950');
$this->template->write_view('content', 'user/profile', $data, TRUE);
On template file '/user/profile.php' use as example:
HTML/PHP template file profile.php:
Your name: <?php echo $data["name"]; ?>
Your name: <?php echo $data["birthdate"]; ?>
And as I see, a CONTENT var must be an ARRAY due to documentation...
$template['default']['regions'] = array(
'header' => array(
'content' => array('<h1>Welcome</h1>','<p>Hello World</p>'), ### <----- AS EXAMPLE
'name' => 'Page Header',
'wrapper' => '<div>',
'attributes' => array('id' => 'header', 'class' => 'clearfix')
)
);
Regions must be defined as template, so if you didn't have header region that didn't work:
$template['default']['regions'] = array(
'header',
'content',
'footer',
);
!!!!!
Simply, he can't acces private access variable _ci_cached_vars which is stored data like $name. RELATED TOPIC: CodeIgniter shared data between calls to load->view
I'm using CakePHP to create a frontend UI for PowerDNS, using a MySQL backend. On the front page of the app I want to have a handful of widgets ('Quickly add a record', 'Quickly add a domain' etc.). One of the widgets I want is a paginated list of existing domains.
The index function in DomainsController.php looks like this:
public $paginate = array(
'fields' => array('id', 'name'),
'limit' => 25,
'order' => array( 'name' => 'asc' ),
'conditions' => array( "NOT" => array( "name LIKE" => "%.arpa" ) )
);
public function index() {
$domains = $this->paginate();
if ( $this->request->is('requested')) {
return $domains;
} else {
$this->set('domains', $domains);
}
}
I've created an element that looks like this:
<?php $domains = $this->requestAction('Domains/index'); ?>
<ol>
<?php foreach( $domains as $domain) :?>
<li>echo $domains['domain']['name']</li>
<?php endforeach; ?>
</ol>
<?php echo $paginator->numbers(); ?>
When I visit the front page, I get an 'Undefinied variable: paginator' error. I've tried using $this->Paginator->numbers() instead but that just gives me 'Undefined property: View::$Paginator'. Adding the 'Paginator' helper to PagesController.php doesn't help either - $this->Paginator becomes available but I get 'Undefined index: pageCount'.
Is it possible to do this kind of pagination from an element on home.ctp or am I going to have to do some custom JavaScript stuff?
EDIT
Now I'm getting somewhere: I changed my DomainsController index function to this:
public function index() {
$domains = $this->paginate();
$paginator = $this->params;
if ( $this->request->is('requested')) {
return compact( 'domains', 'paginator' );
} else {
$this->set('domains', $domains);
}
}
And added the following to the domainList.ctp element:
<?php
$result = $this->requestAction('Domains/index');
$domains = $result['domains'];
$this->Paginator->request = $result['paginator'];
?>
<ol>
<?php foreach( $domains as $domain) :?>
<li>echo $domains['domain']['name']</li>
<?php endforeach; ?>
</ol>
<?php echo $paginator->numbers(); ?>
$this->Paginator is now working properly and I can access all of its methods and properties and so on as normal. My problem now is that if I click on, say, '2', the browser navigates to /pages/home/page:2 but the domain list still shows page 1. Just need to figure out how to pass 'page:2' to the element. And AJAX-ify the whole thing so that I don't need to refresh the whole page.
Firstly, don't do this:
$domains = $this->requestAction('Domains/index');
It's expensive and not good practice and I'm not sure why you need to be doing it from your example.
Secondly, call your paginate like this:
$domains = $this->Paginate('Domain');
OK, I solved this problem, although my solution probably isn't very elegant.
DomainsController.php has a listDomains() function that looks like this:
public function listDomains() {
$domains = $this->paginate();
$paginator = $this->params;
if ( $this->request->is('ajax') ) {
$this->set( 'domains', $domains );
}
if ( $this->request->is('requested')) {
return array( 'domains' => $domains, 'paginator' => $paginator, 'paging' => $this->params['paging'] );
} else {
$this->set( 'domains', $domains );
}
}
home.ctp references an element called domainList.ctp. domainList.ctp, below, in turn uses requestAction() - I know, I know - to call the domainList() function above. Bequest the request is requested, an array containing the values of $domains and $paginator is sent back to the element.
domainList.ctp contains this code:
<?php
$result = $this->requestAction('Domains/listDomains', array('updateId' => 'domainList') );
$domains = $result['domains'];
$paginator = $result['paginator'];
$this->Paginator->request = $paginator;
$this->Paginator->options(array(
'update' => '#domainList',
'evalScripts' => true,
'url' => array('controller' => 'Domains', 'action' => 'listDomains', 'updateId' => 'domainList' ),
));
?>
Essentially what I'm doing here is manually re-populating $this->Paginator->request with the params that were originally sent to the DomainController's domainList() function. This lets me access the various paginator functions, like numbers(), prev() and next(), properly. It's a bit messy but guess what? It gets a little messier.
When you click on the links created by those paginator functions, the 'if ( $this->request->is('ajax') )' segment is executed and the div object on the page is updated with the contents of View/Domains/domainList.ctp instead of View/Elements/domainList.ctp. The contents of View/Domains/domainList.ctp is more or less the same as the corresponding element and the two have to be kep more or less syncronised. The difference is that we don't need to manually populate $this->Paginator:
<?php
$this->Paginator->options(array(
'update' => '#domainList',
'evalScripts' => true,
'url' => array('controller' => 'Domains', 'action' => 'listDomains', 'updateId' => 'domainList' ),
));
?>
Like I said, it's messy and inelegant but it worked for me. I'd be happy to know if anyone has a less kludgy way to do this.