1

I can't add a form to a node. Using the following code, the generated HTML show only the node. If I return $form, the form is rendered correctly. What is my mistake ?

function hook_menu() {

$items['presse/rss'] = array(

'title' => 'RSS',
'access callback' => TRUE,
'page callback' => 'press_rss_page',
'page arguments' => array('fr'),
'menu_name' => 'nav-fast-links-fr',
'file' => 'press.rss.inc',
'type' => MENU_NORMAL_ITEM,

);

FILE: press.rss.inc

function press_rss_page($lang) {

     $nid = variable_get('press_rss_nid_' . $lang, 0);
     $page = array();
     if ($nid) {
        $page = node_view(node_load($nid));
     }

     $path = $_SERVER['HTTP_HOST'];
     $path .= $lang == 'fr' ? '/presse/rss/' : '/press/rss/';
     $form['#weight'] = 1000;
     $form['container'] = array(
        '#prefix' => '<div class="rss-url-block">',
        '#suffix' => '</div>',
     );

     $form['container']['all']['title'] = array(
        '#markup' => '<h3>' . 'All the press room content' . '</h3>',
         );
     $form['container']['all']['text'] = array(
        '#type' => 'textfield',
        '#title' => t('All the feeds', array(), array('context' => 'frontend')),
        '#value' => $path . 'all',
        '#disabled' => TRUE,
        '#id' => 'rss-1',
        '#attributes' => array(
        'onClick' => "this.select();",
        'readonly' => 'readonly',
),

);

  $page['form'] = $form;

   return $page;

}

0

Return your renderable page build with 2 entries, one for the node and one for the form:

return array(
    'node' => $page,
    'form' => $form,
);
  • Yes, it's returned the 2 entries, but I want the form embed in the div of the node – user25139 Jan 10 '14 at 14:18
  • With your solution <div node>page</div><div form>form</div>. I want <div node>page<div form>form</div></div>. – user25139 Jan 10 '14 at 14:20
  • You need to use hook_node_view() for that. Or search through the existing render array to find an appropriate place to inject the form. – Clive Jan 10 '14 at 14:24
  • Based on this drupal_render documentation, I think that added the $form to $page['form'] is ok... but it don't : "Renderable arrays have two kinds of key/value pairs: properties and children. Properties have keys starting with '#' and their values influence how the array will be rendered. Children are all elements whose keys do not start with a '#'. Their values should be renderable arrays themselves, which will be rendered during the rendering of the parent array. The markup provided by the children is typically inserted into the markup generated by the parent array." – user25139 Jan 10 '14 at 15:51
  • That's correct, but you trying to add your $form on an already renderable array build of a node ... which works differently. You need to inject your form in the content part/array of the node_view return. – Mike Vranckx Jan 11 '14 at 10:20

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.