0

I'm creating a page with Hook_menu and I would like to display the user agent that page. This is my code to generate my page.

/**
 * Implements hook_menu().
*/
function custom_module_menu() {
    $items = array();
    $items['custom_module'] = array(
        'title' => 'My Custom Module',
        'description' => t('Best Custom Module'),
        'page callback' => 'custom_module_back',
        'access arguments' => array('administer site configuration'),
    );
    return $items;
}

function custom_module_back(){  
        $output =  array(
            'first_para' => array(
                '#type' => 'markup',
                '#markup' => '<p>Test</p> '  
            ),
        );
        return $output;
    }

I try to set a variable like this $user_agent = $_SERVER['HTTP_USER_AGENT']; and then call it after the p tag.

Like this

function custom_module_back(){  
            $output =  array(
                'first_para' => array(
                    '#type' => 'markup',
                    '#markup' => '<p>Test</p> ' $user_agent, 
                ),
            );
            return $output;
        }

And this did not work.

How can I display a variable into a callback ?

1
  • I am closing this question since the issue is caused by wrong syntax. PHP doesn't recognize '<p>Test</p> ' $user_agent as valid code.
    – apaderno
    Commented Mar 7, 2015 at 8:01

1 Answer 1

2

Based on your code, it should be written as

$output =  array(
  'first_para' => array(
    '#type' => 'markup',
    '#markup' => '<p>Test</p> ' . $user_agent,
  ),
);

rather than

$output =  array(
  'first_para' => array(
    '#type' => 'markup',
    '#markup' => '<p>Test</p> ' $user_agent,
  ),
);

You need the period to concatenate the your string and variable.

Edited - Full working module

/**
 * Implements hook_menu().
 */
function testing_menu() {
  $items = array();
  $items['custom_module'] = array(
    'title' => 'My Custom Module',
    'description' => t('Best Custom Module'),
    'page callback' => 'testing_back',
    'access arguments' => array('administer site configuration'),
  );
  return $items;
}

function testing_back(){  
  $user_agent = $_SERVER['HTTP_USER_AGENT'];
  $output =  array(
    'first_para' => array(
      '#type' => 'markup',
      '#markup' => '<p>Test</p> ' . $user_agent,
    ),
  );
  return $output;
}
3
  • I forgot to post it but I already try this out and did not work. I even re-install the module and I got and Drupal error. Commented Mar 7, 2015 at 7:23
  • Post edited with full module code. What's the error? Commented Mar 7, 2015 at 7:26
  • I was setting up the $user_agent = $_SERVER['HTTP_USER_AGENT']; out-side the callback. Thank you Commented Mar 7, 2015 at 7:28

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