0

7 - How do I programmatically create a button, place it in a block, 'weight it' and then make it visible to certain pages?

Code I've tried:

function openid_connect_block_info() {
  $blocks['openid_connect_login'] = array(
      'info' => t('Login Using PixelPin'),
      'cache' => DRUPAL_CACHE_PER_ROLE | DRUPAL_CACHE_PER_PAGE,
      'region' => 'content',
      'pages' => 'user/',
  );

  $blocks['openid_connect_register'] = array(
      'info' => t('Register Using PixelPin'),
      'cache' => DRUPAL_CACHE_PER_ROLE | DRUPAL_CACHE_PER_PAGE,
      'region' => 'content',
      'pages' => '/user/register',
  );
  return $blocks;
}

This code creates the button but doesn't put it under the content block and doesn't make it only visible on the specified pages.

1 Answer 1

1

Why not render in a tpl so you can control the look of it?

I see you already used hook_block_info() so you can complete this using hook_block_view() with a custom callback function to use hook_theme()

Here is a complete example of it:

function openid_connect_block_info() {
  $blocks['openid_connect_login'] = array(
      'info' => t('Login Using PixelPin'),
      'cache' => DRUPAL_CACHE_PER_ROLE | DRUPAL_CACHE_PER_PAGE,
      'region' => 'content',
      'pages' => 'user/',
  );

  return $blocks;
}

then:

function openid_connect_block_view($delta = '') {
  $block = array();
  switch ($delta) {
    case 'openid_connect_login':
      $block['content'] = openid_connect_login();
      break;
  return $block;
}

then:

function openid_connect_login() {
  return theme('openid_connect_login_tpl', array('data' => NULL));
}

last thing here:

    function openid_connect_theme($existing, $type, $theme, $path) {
      return array(
        'openid_connect_login_tpl' => array(
          'variables' => array('data' => NULL),
          'template' => 'openid-connect-login',
          'path' => drupal_get_path('module', 'openid_connect') . '/theme',
        ),  
);
}

create your file inside you module under the name we gave openid-connect-login.tpl.php and put it in a folder I named theme ..

I hope I understood it right and was helpful for you issue ..

2
  • So do i put openid_connect_theme in it's own file under the theme folder?
    – Callum
    Jan 13, 2017 at 11:19
  • the openid_connect_theme() function should still be in the module file ,, but tpl file (openid-connect-login.tpl.php) should be in the theme folder yes Jan 14, 2017 at 0:49

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.