The following code is a Drupal block made in php.
1) How can I implement more then one item? now i have test1 but i want test1, test2, test3 and test5.
2) how can i link a title for example test1 to my admin/settings/ menu? I want to link an item to node_import in Drupal.
function planning_block($op='list', $delta=0, $edit=array()) {
switch ($op) {
case 'list':
$blocks[0]['info'] = t('Stage administration block');
return $blocks;
case 'view':
$blocks['subject'] = t('Stage administratie');
$blocks['content'] = 'test';
return $blocks;
}
}
If you refer to the documentation of hook_block, you can declare several block inside one hook.
The $delta argument is here to help you differenciate which block your are rendering.
About your links in the title, just use the l() function when you are setting the $block['subject'] value.
Example:
function planning_block($op='list', $delta=0, $edit=array()) {
switch ($op) {
case 'list':
$blocks[0]['info'] = t('Stage administration block 1');
$blocks[1]['info'] = t('Stage administration block 2');
return $blocks;
case 'view':
switch ($delta) {
case 0:
$blocks['subject'] = t('Stage administratie');
$items = array(
l('Item 1', 'admin/settings/1'),
l('Item 2', 'admin/settings/2'),
);
$blocks['content'] = theme_item_list($items);
return $blocks;
case 1:
$blocks['subject'] = l('admin/settings/2', t('Stage administratie 2'));
$blocks['content'] = 'test 2';
return $blocks;
}
}
}
You can either create multiple blocks as shown in Artusamak's answer, or you can simply add more content to $blocks['content'] if you want it in a single block.
$blocks['content'] = l('admin/settings/1', 'test 1') . ' ' . l('admin/settings/2', 'test 2');
Note, if you just want a list of fixed links, you can do that by creating a menu and adding links to it. Every menu is automatically exposed as a block. No custom code required.
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 re-building a website with PHP codeigniter that contains mostly static pages and some dynamic pages interspersed. The problem is, the pages are 1-5 layers deep within the navigation (ex. example.com/about/history/people/person/photos).
I'm not sure how to implement this in the controller. I currently have a crazy set of switch statements nested in switch statements. Here is a simple example:
class About extends MY_Controller
{
//**** INDEX page ****
public function index()
{
$this->setTitle('About');
$this->setDescription('About Stuff');
$this->loadView('about/index');
}
//-------------------------HISTORY section--------------------------------
public function history($sub1 = "", $sub2 = "", $sub3 = "", $sub4 = "")
{
$path = "";
switch($sub1){
//_____ PEOPLE section ____
case 'people':
switch($sub2){
//____ PERSON section ____
case 'person':
switch($sub3){
//**** PHOTOS page ****
case 'photos':
$this->setTitle('Photos');
$this->setDescription('Photo stuff');
$path = 'people/person/photos';
break;
//**** DOCUMENTS page ****
case 'documents':
//Load special scripts
$this->setTitle('Documents');
$this->setDescription('Document stuff');
$path = 'people/person/documents';
break;
//**** INDEX page ****
default:
$this->setTitle('Person');
$this->setDescription('Person stuff');
$path = 'people/person/index';
}
break;
//**** AnotherPerson page *****
case 'anotherPerson':
//Load database
$this->setTitle('AnotherPerson');
$this->setDescription('AnotherPerson stuff');
$path = 'people/anotherperson';
break;
//**** INDEX page ****
default:
$this->setTitle('People');
$this->setDescription('People stuff');
$path = 'people/index';
}
break;
//**** INDEX page ****
default:
$this->setTitle('History');
$this->setDescription('History stuff');
$path = 'index';
}
$this->loadView('about/history/' . $path );
}
}
I feel like I am approaching this incorrectly and it feels messy. So my question is: Is there a better (more compact/clean/dynamic) way of doing this?
You have a few options.
You could put the case statements in separate function. eg. if you've case 'documents' you could put this into a private function:
private function documents(){
//Load special scripts
$this->setTitle('Documents');
$this->setDescription('Document stuff');
$path = 'people/person/documents';
}
You could make an associative array with the title, description etc. in it and check if the variables are filled. After this you can call the array by with the $sub vars.
Is the hook_block has been changed? the following is drupal 6 example i have found on internet(http://eureka.ykyuen.info/2010/11/10/drupal-create-a-block/), there is no block shown in admin/structure/block,
* Implementation of hook_block().
*/
function custom_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
//Define the block
case 'list':
$blocks[0]['info'] = t('Block Info');
$blocks[0]['cache'] = BLOCK_NO_CACHE;
return $blocks;
case 'configure':
//TODO: block configurable parameters
$form = array();
return $form;
case 'save':
//TODO: save new configuration
return;
//Display the block
case 'view':
$block['subject'] = t('Block Subject');
$block['content'] = 'Block Content';
return $block;
}
}
it seems that, hook_block in drupal 7 has been changed, how to rewrite about code? anyone can provide hints/direction to me? thank you very much.
In Drupal 7, your implementation of hook_block() would be changed to:
/**
* Implements hook_block_info().
*/
function custom_block_info() {
$blocks = array();
$blocks['list'] = array(
'info' => t('Block Info'),
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function custom_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'list':
if (user_access('access content')) {
$block['subject'] = t('Block Subject');
$block['content'] = 'Block Content';
}
break;
}
return $block;
}
Your code works for Drupal 6.The implementation of hook_block is changed in Drupal 7.
In Drupal 7 there are different hooks that should be used to serve your purpose.
hook_block_configure
hook_block_info
hook_block_save
hook_block_view
Check more about hook_block here
How to embed node add form in a block?
I have tried the following but it does not work. "free_listing2_node_form" is the form_id of the node add form I want to embed in this block.
If the approach below is right, I suspect problem in this statement
$block['content'] = drupal_get_form('free_listing2_node_form');
Any help / direction is much appreciated!
<?php
function freelisting2_block_info() {
$blocks['neil_recent'] = array(
'info' => t('neil_Recent content'),
);
return $blocks;
}
function freelisting2_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'neil_recent':
if (user_access('access content')) {
$block['subject'] = t('Recent content');
$block['content'] = drupal_get_form('free_listing2_node_form');
}
break;
}
return $block;
}
?>
(I am using Drupal 7)
Try to use this:
$block['content'] = render(drupal_get_form('free_listing2_node_form'));
I didn't test it.
I'm using hook_block to create a block with the name of the custom module I'm creating.
I'm not being able to create a block without using myModuleName_block.
Do I need to do different modules for every different block I want to create?
You can make several blocks with hook_block, just use the $delta.
function hook_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$blocks[0]['info'] = t('Block 1');
$blocks[1]['info'] = t('Block 2');
return $blocks;
case 'configure':
if ($delta == 0) {
// Block 1
}
else if ($delta == 1) {
// Block 1
}
....
}