0

Me again with the noob question.
I want to achieve this:
At the url http://ROOT/something/{numeric_id_here} to get a page that lists the header and the footer of the theme and my custom content. The content is not manage-able through the admin. It depends on the numeric id.
Here is what I got so far.
I use the hook_menu for this:

function something_menu() {
    $items['something/%id_here'] = array(
        'title' => 'Some title',
        'type' => MENU_NORMAL_ITEM,
    );
    return $items;
}

I have the function that parses the url and gets the numerical value:

function id_here_load($arg) {
    $data = get my custom data from an external xml feed based on $arg; //this works nicelly - I get an array with the data I need.
    //WHAT'S NEXT?
}

Now I'm stuck. how can I pass $data to a template and render that template in the content of the page?
My question is somehow similar to this one. Just that the answers provided there don't cut it for me.

  • check out the hook_menu() docs api.drupal.org/api/drupal/modules%21system%21system.api.php/… – Andrew Welch Nov 27 '14 at 14:27
  • @AndrewWelch I've been on that page at least 5 times. Looks like half Chinese to me. I forgot to mention that my drupal experience barely adds up to 2 man-days of work. Any suggestions or pieces of code I could try? – Marius Nov 27 '14 at 14:30
  • if you want to do an ajax call in your callback check out something like this drupal.org/node/2046693 – Andrew Welch Nov 27 '14 at 14:30
  • I don't need an ajax call. I need to list in the page http://ROOT/something/{numeric_id_here} the header and footer of my theme and the center part must come from a certain template. – Marius Nov 27 '14 at 14:31
  • why don't you just use views then? drupal.org/project/views it allows you to set the template and I'm sure you could rewrite the url or just use the node id – Andrew Welch Nov 27 '14 at 14:33
1

Something like this. Do your hook menu with (optional) arguments. In the callback do an entityFieldQuery (or pull in your XML). Use hook_theme to specify a custom template. this code isn't tested and will need some editing / adapting for your use context.

If you prefer to pull in a feed maybe https://www.drupal.org/project/feeds is what you are looking for.

//menu

/**
     * Implements hook_menu().
     */
    function example_menu() {
      $items = array();
      $items['example_object/something/%'] = array(
        'title' => t('callback'),
        'page callback' => '_example_mystuff',
        'page arguments' => array(
          0,
          1,
          2
        ),
        'access arguments' => array(
          ''
        ),
        'type' => MENU_CALLBACK
      );
      return $items;
    }

//callback

     function _example_mystuff($arg1, $arg2, $arg3) {
        $query = new EntityFieldQuery();
//or load in your xml
        //do an entity field query to look up your items
         $results = $query->execute();
        return $results;
        }

//theme function

 function example_theme($existing, $type, $theme, $path) {
      return array(
    '_example_mystuff' => array(
          'template' => 'example-theme',
          'path' => $path . '/templates',
          'variables' => array(
            'variables' => NULL
          )
        )
    }
  • 1
    That's more like it. Explained for 5 year olds. Works perfectly. I needed to adjust some parameters but exactly what I was hoping for. – Marius Nov 27 '14 at 14:58
2

You need more in your hook_menu() like this:

function something_menu() {
  $items = array();

  $items['something/%id_here'] = array(
    'title' => 'Some title',
    'type' => MENU_NORMAL_ITEM,
    // This is the function that will return your page content.
    'page callback' => 'something_custom_page',
    // This will pass the $data variable you returned from your loader function
    // into your page callback (not the ID in the URL).
    'page arguments' => array(1),
  );

  return $items;
}

Then your load function needs to actually return what it has loaded, like this:

function id_here_load($arg) {
  // Do some checks o make sure no one can pass in bad data via the url.
  // This is an example you need to check based on your data.
  if (is_numeric($arg)) {
    $data = get my custom data from an external xml feed based on $arg; //this works nicelly - I get an array with the data I need.
    // If you get back valid data.
    // Again you need to make this relevant to your data.
    if ($data) {
      // Return the data.
      return $data;
    }
  }
  // If for any reason you didn't end up getting data return FALSE.
  // This will cause access denied for this page because of an invalid ID or something.
  return FALSE;
}

Then you need to implement your page callback function like this:

function something_custom_page($data = array()) {
  // Do something with the $data to create the page and return
  // either a string of HTML or a drupal render array.
  // Returning a render array is the preferred option.
}

See Render Arrays in Drupal 7 form more info on render arrays.

| improve this answer | |
  • Thanks. This works. But since it's kind of the same thing that Andrew answered and he answered first, I accepted his answer. Anyway +1 from me. – Marius Nov 27 '14 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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