1

I created a very simple module which generates a tag cloud based on user input; you can see the code here.

// wow.module
function wow_menu() {
    $items = array();
    $items['wall/of/words'] = array(
        'title' => 'Wall of Words',
        'page callback' => 'wow_page',
        'access arguments' => array('access content'),
        'file' => 'wow.inc',
        'weight' => 1,
    );
    $items['admin/settings/wall/of/words'] = array(
        'title' => 'On this date module settings',
        'description' => 'Description of your On this date settings page',
        'page callback' => 'wow_admin_page',
        'page arguments' => array('wow_admin'),
        'access arguments' => array('administer wow settings'),
        'type' => MENU_NORMAL_ITEM,
    );
    return $items;
}

function wow_admin_page() {
    return 'hey';
}



// wow.inc
function wow_page() {
    $output = array(
        'form' => drupal_get_form('wow_cake'),
        'words' => _getWords(),
        'min_count' => 0,
        'max_count' => 999
    );
    drupal_add_js(drupal_get_path('module', 'wow') . '/js/jquery.tagcloud.js');
    drupal_add_js('jQuery(function(){jQuery("#wow-container .cloud a").tagcloud({size:{start:20,end:40,unit:"pt"},color:{start:"#46A7E3",end:"#0969A3"}})})', 'inline');
    return theme('wow_template', array('output' => $output));
}

function wow_theme($existing, $type, $theme, $path) {
    return array(
        'wow_template' => array(
            'template' => 'wow_template'
        ),
    );
}

function wow_cake($form, &$form_state) {
    $form = array();

    $form['title'] = array(
        '#type' => 'textfield',
        '#title' => t('Thank you for...'),
        '#size' => 60,
        '#maxlength' => 128,
        '#required' => TRUE,
    );

    $form['buttons']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Submit'),
    );
    return $form;
}

function wow_cake_validate(&$form, &$form_state) {

    if (empty($form_state['values']['title'])) {
        form_set_error('title', t('Please leave a thank you message in highlighted field below.'));
    }
}

function wow_cake_submit(&$form, &$form_state) {

    if (_create_node($form_state['values']['title'], $form_state['values']['title'])) {
        drupal_set_message(t('Thank you for your kind words!'), 'status');
    }
}

function _getWords() {
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'node', '=')
            ->propertyCondition('status', 1, '=')
            ->propertyCondition('type', 'wall_of_words', '=');
    $result = $query->execute();
    $nodes = $result['node'];
    $fields = field_info_instances('node', 'wall_of_words');
    $field_id = $fields['body']['field_id'];
    field_attach_load('node', $nodes, FIELD_LOAD_CURRENT, array('field_id' => $field_id));
    $wall = '';

    foreach ($nodes as $node) {

        if (!empty($node->body['und']['0']['value'])) {

            $wall .= trim($node->body['und']['0']['value']);
            $wall .= " ";
        }
    }
    if (!empty($wall)) {
        $cloud = explode(" ", $wall);
    }

    return array_count_values($cloud);
}

function _create_node($title = "", $body = "", $language = "und") {

    $node = (object) array();
    $node->type = 'wall_of_words';
    $node->status = 1;
    $node->promote = 0;
    $node->sticky = 0;
    $node->revision = 0;
    $node->language = $language;
    $node->title = $title;
    $node->body[$language][0] = array(
        'value' => $body,
        'format' => 'full_html',
    );
    if ($node = node_submit($node)) {
        node_save($node);
    }

    return $node;
}

My issue: when I visit another page and come back to the page that I generated with my module the output of the content does not get displayed unless I clear the cache and visit the page again.

0

2 Answers 2

0

there's this method here :

drupal_page_is_cacheable

actually i never used it, but cacheexclude module does, maybe you have to hook_init too and check for your path there !

6
  • I tried the cache exclude module and it didn't seem to fix the problem, not sure if its related to cache or an issue with my module's code.
    – user990717
    Feb 14, 2013 at 15:55
  • could you add your path in cacheexclude ?
    – rémy
    Feb 14, 2013 at 15:56
  • wall/of/words ?
    – rémy
    Feb 14, 2013 at 15:57
  • yup, added it, cleared cache after too
    – user990717
    Feb 14, 2013 at 16:16
  • give $GLOBALS['conf']['cache'] = FALSE; a try
    – rémy
    Feb 14, 2013 at 16:27
0

If you get different results between generating a page from scratch and fetching from cache, then that generally means that something in the page generation cycle is not stored in cache. The fact that cacheexclude does not solve the problem suggests that it isn't the page cache that's the problem.

One thing I note is that you are declaring a template for your output, but it's not shown. The declaration in your hook_theme() function is missing the variables argument, so this may well be a theme-related problem.

On a stylistic note, all functions in a module should be named either modulename_otherstuff or _modulename_otherstuff, to minimise the chance of duplicates.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.