php - Drupal custom modules and cache issue -



php - Drupal custom modules and cache issue -

we created few custom custom modules drupal site , have next issue : each time create or update content (whatever is), content generated custom modules disappears. have clear caches content appear again.

as our first experience drupal, missing don't know what.

any help appreciated!

below code of 1 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, ); homecoming $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; } homecoming $block; } /** * callback implemented 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 $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'; homecoming theme('website_actualites_output', array( 'title' => $static_title, 'content' => $static_content, 'data' => $data ) ); }

file website_actualite-sweet--block.tpl.php

<?php foreach ($data &$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"> <a href="/'. $url .'"><img src="' . $imageurl . '" class="img-responsive" alt="image description"></a> </div> <p><a href="/'. $url .'">' . $row['title'] . '</a></p> </div>'; } }

first of there several problems code. if you're going utilize drupal need stick drupal coding standards. avoid using camel casing variables, instead should utilize underscores. should utilize 2 spaces indentation.

in block info function define single block assign unassigned variable:

// define $blocks first. $blocks = array(); $blocks['website_actualites'] = array(

next issue phone call hook_block_view($delta), phone call fired every block delta, , regardless of block delta setting null (also future reference should null):

$block = null;

so drupal building render array each block, , wiping data. doing because trying utilize block delta way pass parameter block delta website_actualites+10. blocks aren't designed work way , delta's meant static drupal can maintain track of them in database , perform right caching according cache flag. you've defined delta website_actualites in hook_block_info() drupal has no knowledge or configuration info other block delta.

if have need same block display varying quantities, define several blocks (in hook_block_info()) , phone call same helper function now. if there requirement configure block should utilize hook_block_configure() api

now info passing theme function, extracting field info node object using: $item->field_image['und'][0]. couple of things here: fields provided field module has extensive api retrieve field data. should utilize field_get_items() instead, homecoming array of items associated field entity (as fields can multi-valued) , handles language you. other thing never utilize 'und' utilize 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 set $block['content'] result of theme('website_actualites_output', ...). function homecoming markup, drupal has many more calls in chain before need render markup, , problem calling can't mutate info @ point in build process.

in order phone call theme('website_actualites_output', ...); you need have defined theme function in hook_theme() call, presumably have done in module, fine remember add together dependency on module in module.info file. want doing adding instructions drupal build, not markup:

return array( '#theme' => 'website_actualites_output', '#title' => $static_title, '#content' => $static_content, '#data' => $data );

so come template, have lot of logic in template , intended displaying content markup, ideally no, or minimal computation if any. theme function has hook_preprocess() phone call before hook_process(), , variables passed template.

you using theme function loop on $data variable display markup - ideally theme function markup variables processed , passed 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 illustration best bet have sec theme function website_actualites_output__child above template.

/** * implements hook_theme(). */ function website_actualites_theme() { $templates = drupal_get_path('module', 'website_actualites') . '/templates'; homecoming 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 current theme function:

/** * implements hook_preprocess_hook(). */ function website_actualites_preprocess_website_actualites_output($variables) { $processed_output = array(); $data = $variables['data']; foreach ($data $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 $data variable can render() it. // var_dump($processed_output) improve thought what's going on here. $variables['data'] = $processed_output; }

then need in current template to:

<?php if ($data): ?> <?php print render($data); ?> <?php endif; ?>

and can wrap whatever markup like.

the drupal learning curve high, remember if else maintain code after you, expect adhere drupal coding standards , followed process upon drupal built.

further reading: render arrays in drupal 7

hope info helps , makes bit clearer.

php drupal drupal-7 drupal-modules

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -