1

Using Views in Drupal 7 I can consume the output of a restful API. I want to take some of the data and dynamically create pages. For example I get the following return json:

$data = {'title':'This is a page title','alias':'pages/page-alias-name','content':'This is some content'}

I would want to dynamically create a page located at http://www.example.com/pages/page-alias-name which would have a title of $data['title'] and the content would be $data['content']. Is it possible to create pages dynamically like this. How can I go about doing this?

Additionally, if I had identical content stored in an external database that can be read by a custom Views module, can I do this and how would I go about it?

  • Do you have a way to get all of the data if given the alias? Eg, in a menu callback if you can call a service that returns data given the alias, this is a trivial matter. – Jimajamma Feb 22 '14 at 3:21
  • Yes, given an alias I can call the info dynamically. – John Feb 22 '14 at 9:01
1

I'm not sure how you are using Views to do this, but with a custom module you could easily do this with a fairly simple hook_menu(), eg, something along these lines:

function YOURMODULE_menu() {

  $items=array();

  $items['pages'] = array(
    'page callback' => 'YOURMODULE_get_some_rest',
    'access callback' => TRUE,
  );

  // any other menu items go here

  return $items;

}

and then:

function YOURMODULE_get_some_rest() {

  $args=func_get_args();

  if ($args) {

    $alias='pages/' . implode('/', $args); // so pages/foo works as does pages/foo/bar etc

    $data = /* get your data from your external service */ ;

    if ( /* $data was found */ ) {

      drupal_set_title( /* title in $data */ );

      return /* content in $data */ ;

    } else {

      return MENU_NOT_FOUND; // we don't have an alias of that name

    }

  } else {

    return MENU_NOT_FOUND;  // just called 'pages' so do something else here if need be, perhaps a list of all pages but for now, we just return not found

  }

}

so after a cache clear, users should now be able to go to www.example.com/pages/alias and Drupal will go out to your external service and grab the content and if found, populate the page with it, otherwise will return not found.

Now, if this data is going to be fairly static, I'd look into caching it so your users aren't delayed while Drupal fetches it. For that, I'd look into cache_set() and cache_get().

ADDITION:

Drupal's menu system works on a sort of best fit methodology. For example, if you define a menu item foo, it will obviously match www.example.com/foo, but, if no menu item has been defined for foo/bar, it will also match www.example.com/foo/bar or even www.example.com/foo/bar/baz/bing etc etc.

So, in my example above, I defined pages. This will match www.example.com/pages which is just an error condition above, but with some programming could list all of the potential pages you have available to you, but also will match www.example.com/pages/alias-of-page or www.example.com/pages/alias/of/page.

Inside the callback, I use PHP's built-in function func_get_args() to deal with the variable number of arguments that can be passed to this function. When you go to www.example.com/pages, no arguments will be passed to this callback and func_get_args() will return an empty array. But, when you go to www.example.com/pages/alias-to-page, Drupal will automatically pass alias-to-page to the callback, and func_get_args() will return an array containing it, and when you go to www.example.com/pages/alias/to/page, it will pass three arguments, alias, to, and page to the callback function and func_get_args() will return an array containing all three.

As to your commentary question regarding this inside Views, once again, I'm not sure how your are doing this inside Views, but yes, this would go inside YOURMODULE.module and would stand alone outside of Views (although Views might be a great place to generate the listing on pages by itself).

  • I am a bit confused. What is $args taking as I can't see any function arguments being fed in. I also assume that the first function is put in the .module file? Sorry for the confusion I am very new at this. Also, how can I adjust this to take the value from a custom database feed using the $data from hooks_view_data? – John Feb 23 '14 at 11:51

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.