Here is my custom.info
name = Custom
description = Custom module
core = 7.x
package = Own
and custom.module
<?php
/**
* #file
* An example custom module for selecting, updating and deleting query
*/
/**
* Implementation of hook_block_info()
*/
echo 'Today: \n';
echo date('m/d/Y');
function custom_block_info() {
$block['custom'] = array('info' => t('Custom block'))
return $block;
}
/**
* Implements hook_block_view.
*/
function custom_block_view($delta = '') {
global $user;
$block['content'] = t('Hello #user from IP #host',array(
'#user' => format_username($user),
'#host' => $user->hostname`enter code here`
));
$result = db_select('node','a')
->fields('a', array('title'))
->execute();
foreach($result as $node) {
$items[] = array(
'data' => t($node->title)
);
}
$block['content'] .= theme('item_list', array(
'items' => $items
));
return $block;
}
But this custom module is not displaying data in the sidebar where had i put the block. i have place the echo statement above the code it's not even displaying that echo statement in block can any one tell me how to resolve this????
P.S. I have installed drupal i ve changed nothing in database!
Check if your module is active in the modules list (admin/modules) if so try to put your echo statements in a hook_init like this:
function custom_init(){
echo 'Today: \n';
echo date('m/d/Y');
}
then clear all drupal cache Configuration > Performance > Clear alla caches (admin/config/development/performance)
Related
I found this code online and I would like to implement it. However, I have never worked with hook functions.
My question is when I put this code into a brand new php file ex: uc_microcartTest.php .
How do I call this new php file and get the results to show like this?
/**
* Implementation of hook_block().
*/
function uc_microcart_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$blocks[0] = array(
'info' => t('Micro-sized cart block for page header.'),
// This block cannot be cached, because anonymous
// sessions can have differing cart contents.
// To improve this, see drupal.org/project/uc_ajax_cart
'cache' => BLOCK_NO_CACHE,
);
return $blocks;
case 'view':
if ($item_count = uc_cart_get_total_qty()) {
$block = array();
$block['subject'] = '';
$block['content'] = theme('image',
drupal_get_path('module', 'uc_cart') .'/images/cart-full.png');
$block['content'] .= format_plural($item_count,
'My cart: 1 item', 'My cart: #count items');
$block['content'] = l($block['content'], 'cart', array('html' => TRUE));
return $block;
}
break;
}
}
Although it is possible to call the functions from external PHP files but I recommend you follow the "Drupal way":
Create your own custom module
Implement hook functions in <your_module_name>.module file.
Enable your module
You should also read more about how hooks work in Drupal here
I am new at Drupal 7 and I'm creating a Block by code, following this tutorial.
So I create a new module folder at drupal/sites/all/modules and created two files:
block_square_menu.info: it has the info of the module:
name = Block Square Menu
description = Module that create a Block for Square menu, menu shown only in home page
core = 7.x
package = custom
block_square_menu.module: it contains the PHP code:
<?php
/**
* Implements hook_block_info().
*/
function block_square_block_info() {
$blocks = array();
$blocks['block_square'] = array(
'info' => t('Block Square'),
'cache' => DRUPAL_CACHE_PER_ROLE,
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function block_square_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'block_square':
$block['subject'] = t('block Title');
$block['content'] = t('Hello World!');
break;
}
return $block;
}
After save the files, I go to Admin/Modules, I activate the new module and save the configuration. Now I go to Structure/Blocks and it should list my new Block, but it doesn't do.
I have followed all the tutorial steps and I cleaned Drupal cache, but I'm still having the problem.
First solve your mistake: change the function name where you implemented hook_block_view(), you need to change it as function blocks_square_block_view()
/**
* Implements hook_block_view().
*/
function blocks_square_block_view($delta = '') {
$block = array();
......
After also if not solve then remove 'cache' attribute from hook_block_info() it is optional.
Then follow 2 steps if you missed.
1) Clear all cache (/admin/config/development/performance).
2) Enable your custom module (/admin/modules).
After trying again, your block should appear in (/admin/structure/block).
Solved, the problem was the name of the functions. So the names started with "block_square" which it have the word "block" and it causes some trouble so I changed the all the names with menu_square.
So the functions are now:
menu_square_block_info()
menu_square_block_view($delta = '')
And the files are:
menu_square.info
menu_square.module
The code of the files are:
info:
name = Menu Square
description = Module that create a Block for Square menu, menu shown only in home page
core = 7.x
package = custom
module:
<?php
/**
* Implements hook_block_info().
*/
function menu_square_block_info() {
$blocks['menu_square'] = array(
'info' => t('Block Square'),
//'cache' => DRUPAL_CACHE_PER_ROLE,
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function menu_square_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'menu_square':
$block['subject'] = t('block Title');
$block['content'] = t('Hello World!');
break;
}
return $block;
}
In my drupal 8 custom module I use block to show the next and previous links of current article page. However, links do not change when switching nodes due to the caching. How can I limit the caching for this block?
I can't wrap my head around this.
public function build() {
/**
* {#inheritdoc}
*/
$node = \Drupal::request()->attributes->get('node');
$created_time = $node->getCreatedTime();
$nextprevlinks ="";
$nextprevlinks .= $this->generateNext($created_time);
$nextprevlinks .= $this->generatePrevious($created_time);
return array('#markup' => $nextprevlinks);
}
Just in case someone else brain farts like I just did.
This is how my return now looks:
return array('#markup' => $nextprevlinks,
'#cache' => array("max-age" => 0),
);
Ok, so I'm very new to Drupal and I'm trying to create a custom module that generates a page that the user can navigate to. When the user clicks on the page, I'm trying to retrieve the titles of all the content pieces from the database and arrange them in a table using table_theme. Everything is working except the generating of the rows in the table. It's giving me this message at the top of the page:
"Warning: Invalid argument supplied for foreach() in theme_table() (line 2107 of /srv/bindings/4d0bbd3c7be847abb26f62e0dacbcc24/code/includes/theme.inc)."
This message appears once for each title that I retrieve from the database. Here is my content_titles.module file (my custom module file):
<?php
/**
* #file
* A module that creates a page to display all existing content titles
*/
/**
* Implements hook_help()
*
* Displays help and module information
*
* #param path
* Which path of the site we're using to display help
* #param arg
* Array that holds the current path as returned from arg() function
*/
function content_titles_help($path, $arg) {
switch ($path) {
case "admin/help#content_titles":
return '' . t("Displays all existing content titles
on an overview page") . '';
break;
}
}
/**
* Implements hook_menu()
*
* Enables the module to register a link to be placed in a menu
*/
function content_titles_menu() {
$items['test/content_titles'] = array(
'title' => 'Overview Test Page',
'page callback' => 'content_titles_simple',
'access callback' => TRUE,
'expanded' => TRUE,
);
return $items;
}
/**
* Constructs the Content Titles page
*/
function content_titles_simple() {
return array('#markup' => create_content_table());
}
/**
* This function will create the table to hold the content titles in
*/
function create_content_table() {
// Retrieve content titles from the database
$results = db_query('SELECT title FROM {node}');
$header = array('Content Titles');
$rows = array();
while ($row = $results->fetchAssoc()) {
$rows[] = $row['title'];
}
print_r($rows); // FOR DEBUGGING PURPOSES
$output = theme('table', array('header' => $header,
'rows' => $rows));
return $output;
}
The problem appears to be with my use of the theme function. I don't see how any of my arguments are invalid though. I'm passing the theme type of 'table' and two arrays that I've checked aren't empty (that's why I use print_r to print the array that I store my titles from the database in). I'm pretty stumped here. Any idea what the problem could be?
Thanks for the help everyone, I figured it out! I needed to push arrays onto the $rows array instead of the values. I changed this section of code:
$rows = array();
while ($row = $results->fetchAssoc()) {
$rows[] = $row['title'];
}
To:
$rows = array();
while ($row = $results->fetchAssoc()) {
$rows[] = array($row['title']);
}
You must use as below:
$header = array(
array('data' => t('Content Titles'), 'field' => 'title'),
);
$rows = array();
$query = db_select('node', 'n')->fields('n', array('title'));
$pager = $query->extend('PagerDefault')
->limit(10);
$results = $pager->execute();
foreach ($results as $row) {
$rows[] = array($row->title);
}
return theme('table', array(
'header' => $header,
'rows' => $rows)) . theme('pager');
Thanks
I need to put some code into the site on drupal and i need this code to work on every page of my site. How can i do this? I wanted to find the file of footer and put some code inside, but i can't find it.
There are 3 ways to add PHP code in to the footer.
1> Turn on the PHP filter & enter the code into a block that is positioned in the footer left region.
2> Put the code in the appropriate template file in the sub-theme.
3> Make a module that outputs the code to a block; activate and place the block.
Suppose that you want to add following line to footer area :
©<?php print date('Y');?> Your Company Name - Address of your company.
So best way to do it is make small module like this:
copyright_block.info
name = Copyright Block
description = Shows the (incrementing) current year and company information.
package = Other
core = 7.x
files[] = copyright_block.module
copyright_block.module
<?php
/**
* #file
* This module shows the copyright year and company information.
*/
/**
* Implements hook_help().
*/
function copyright_block_help($path, $arg) {
if ($path == 'admin/help#copyright_block') {
return t('Manually edit to change company information');
}
}
/**
* Implements hook_block_info().
*/
function copyright_block_block_info() {
$blocks = array();
$blocks['show_copyright'] = array(
'info' => t('Company Information'),
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function copyright_block_block_view($block_name = '') {
if ($block_name == 'show_copyright') {
$content = "<p>©" . date('Y') ." Your Company Name - Address of your company</p>";
$block = array(
'subject' => t('Company Information'),
'content' => $content,
);
return $block;
}
}
NOTE: Do not put PHP end tag ?> at the end.