1

Hi I am newbie in Drupal, I have problem about theming.

I have a PHP file ads.php that contains random advertisements now just wondering what is the perfect way to include that ads.php

Can I just use the PHP include command


include('ads.php');

or import it as a block for example block--ads.php or region--ads.php I DON'T KNOW IF THIS IS POSSIBLE. I am still confused on how drupal theming works.

Please help me.

  • you can use include – Nabil Mar 4 '14 at 12:06
3

You need to create a block template in your themes template folder.

To do this, you would create a new block, in Structure > Blocks, give it a bit of dummy content and save it in a region.

Then inspect the ID of the block in the source, or in Chrome Dev Tools, or Firebug, it should be something like block-block-1

In your themes template folder, copy the file block.tpl.php and rename the copied file block--block--1.tpl.php

In that file, you should see something like...

<div class="content"<?php print $content_attributes; ?>>
    <?php print $content ?>
  </div>

Just replace the <?php print $content ?> with your content from ads.php

Then, in Performance > Development, flush the cache, and you should see your template replace the content of the block you created.

0

You can enable the PHP Filter core module and add your php code directly to the block.

  1. Enable the module
  2. Create a new block
  3. Change the text format to PHP Filter
  4. add your PHP code to the block body.
  5. Save it.
  6. Clear your cache.
  • 1
    This is the easier option, but allowing php to be entered into your database can expose some serious Security issues. The OP asked for the perfect way to do it, and using the correct Drupal template process is much better practice. :) – Collins Mar 4 '14 at 13:56
  • I don't disagree with you. – Scott Joudry Mar 4 '14 at 16:24
0

The answer is to create block but on own module. This way will prevent block from deletion by the UI.

Creating Drupal-7.x module

Use hook_block_info and hook_block_view - see more for them on api.drupal.org

hook_block_info is trivial, so just replace with your info.

ex. of hook_block_view

function hook_block_view($delta = '') {
  $block = array();

  switch ($delta) {
    case 'my_block':
      $block['subject'] = t('My block');
      $block['content'] = awesome_ads();
      break;

  return $block;
}

function awesome_ads() {
  $output = '';

  // your code here

  return $output;
}

Then enable your module and show "My blok" on block UI.

edit:

To include your php file, use instead '// your code here' on function above

module_load_include('inc', 'my_module', 'ads');

Place ads.inc file with your php code in my_module folder (if name of your module is my_module)

0

From what I understand, you want to output some custom content on a page (some ads, in this case).

This isn't something you should do at the theme layer, as the theme layer is there for changing markup and appearance, not adding content. The general rule is: if there isn't already a module that does this for you, you'll need to create one.

There's documentation here on how to create a module: https://drupal.org/developing/modules/7 It's only a couple of files to get started.

If you download the Examples module, you'll find examples of how to create blocks in a module. You'll be shown how to use hook_block_info() and hook_block_view() to create your block. Once you're up and running you'll be able to make it appear in whichever region you need.

There's some great information, too, about writing secure code. Use of the PHP filter isn't generally encouraged, as it's seen as a security risk. It will be also worth checking that the PHP for the ads that you're including is well written and secure.

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.