0

I would like to check if particular node instance exists then load that node. if the instance does not exist, then display message. So far:

function MODULE_menu() {
    $items['ds-some'] = array(
        'title' => 'Some',
        'page callback' => 'some_display',
        'access arguments' => array('access content'),
        'type' => MENU_NORMAL_ITEM,
        'file' => 'inc/some.pages.inc',
    );

    return $items;
}

function MODULE_theme(){
    return array(
        'render-some' => array(
            'template' => 'page--some',
            'variables' => array(
                'node' => NULL
            ),
            'path' => path_to_theme().'/templates',
        ),
    );
}

in file inc/some.pages.inc

    function some_display(){
            $build = array();

            $nid_some = db_select('node', 'n')
                ->fields('n', array('nid'))
                ->fields('n', array('type'))
                ->condition('n.type', 'SOME_TYPE')
                ->execute()
                ->fetchCol();

            //load first instance 
            $someNode = node_load($nid_some[0]);

            if (!empty($shopNode)) {

//Attempt 1
    //            $build['ds-some'] = array(
    //                '#theme' => 'node',
    //                '#node' => $someNode,
    //                '#view_mode' => 'full',
    //            );

//Attempt 2    
    //            $build['ds-some'] = node_view($someNode, 'full');

                $build['ds-some'] = array(
                    '#theme' => 'render-some',
                    '#node' => $someNode,
                );
            }
            else {
                drupal_set_message(t('It appears there is no instance present');
            }
            return $build;
    }

The first two attempts ended with the title of the Node instance but Drupal is no rendering the whole page(template file, views, etc). In my last attempt, i use custom theme function that it works, however. It is not rendering the context for the node type specified. How to specify context when working with hook_menu(), so that the context configured will load blocks and views? Thank you for any help.

4
  • what is context configured? Are talking about Context module?
    – xurshid29
    Commented Jan 28, 2014 at 5:54
  • Yes, sorry. The Context module and then the node that i am trying to render via hook_menu() is configured to 'Node Type'
    – latvian
    Commented Jan 28, 2014 at 6:48
  • 1
    As side note, the template filename for render-some cannot be page--some. Also, you cannot use render-some as theme function identifier, since the hyphen cannot be part of a PHP function name: "A valid function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores."
    – avpaderno
    Commented Jan 28, 2014 at 7:27
  • kiamlaluno, i have $vars['theme_hook_suggestions'][] = 'page__'.$type; in the hook_preprocess_page function. The custom theme function with that name seems to be working. thank you
    – latvian
    Commented Jan 28, 2014 at 14:02

3 Answers 3

1

I don't know how the Context module handles Entity types, but it loads entity type definitions using the entity ID (from the URL). In your case, you are creating a menu item without any arguments, just specifying business logic in the callback, and your menu item path is not a node path. That's why the Context module can't find any entity type definition. In this case you can specify additional context reactions by path (by your custom path) and display blocks.

You don't need to specify double fields method; you can write much simpler code.

$nid_some = db_select('node', 'n')
  ->fields('n', array('nid', 'type'))
  ->condition('n.type', 'SOME_TYPE')
  ->execute()
  ->fetchAll();

If you need to fetch only one row from the database, you can use the following code.

$nid_some = db_select('node', 'n')
  ->fields('n', array('nid', 'type'))
  ->condition('n.type', 'SOME_TYPE')
  ->range(0,1)
  ->orderBy('nid', 'DESC') // Additional sorting
  ->execute()
  ->fetchAll();
2
  • Thank you. Can i pass the type to the Hook_menu() to kick in the context? I tried different ways, but i am unable to do(.i.e. 'page arguments' => array('node', 22), 'page arguments' => array('type', 'some'),etc)
    – latvian
    Commented Jan 28, 2014 at 14:59
  • I don't think that context will react to that argument.. Alternatively you can try to create custom context reaction by your needs. Here is a tutorial: dtek.net/blog/…
    – xurshid29
    Commented Jan 28, 2014 at 15:07
1

I will answer to the part that is not about the Context module, since the hook_theme() implementation is not correct.

render-some is not a correct theme function identifier, since that needs to be an identifier PHP is able to use as function name or as part of a function name. Even when the theme implementation is done in a template file, and not in a function, Drupal will always use the identifier to find some functions required for the theme implementation, such as:

  • template_preprocess_render-some(), the preprocess function implemented from your module
  • hook_preprocess_render-some(), the preprocess function implemented by other modules
  • template_process_render-some(), the process function implemented from your module
  • hook_process_render-some(), the process function implemented by other modules

Even if you don't plan to use preprocess or process functions for that template file, using render-some as theme identified doesn't allow other modules to implement those preprocess or process functions, since the function name Drupal would look for isn't a function name allowed from PHP.

2
  • Thank you for clarifying. As long it works for me, i don't see why i cannot use the identifier 'render-some' for custom theme function...perhaps, its not exactly php function but just a reference, so the PHP interpreter is not complaining
    – latvian
    Commented Jan 28, 2014 at 14:12
  • The PHP interpreter doesn't complain, as long as you don't try to add a preprocess or a process function to that theme implementation; in that case, you should add a function whose name is template_preprocess_render-some(), which is not a function name PHP allows. Even if you don't plan to add a preprocess or a process function, you are still not allowing other modules to add a preprocess/process function to that theme implementation, which could be necessary to change some of the values passed in the $variables array passed to the template file.
    – avpaderno
    Commented Nov 5, 2019 at 9:49
0

To answer how to activate context, here is snippet:

$context = context_load('NAME_OF_CONTEXT');
context_set('context', $context->name, $context);

For complete info on how to render the page programmatically, activate context, etc in this post http://margotskapacs.com/2014/02/rendering-page-programmatically-to-create-permanent-urls-like-shop-gallery/

Your Answer

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

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