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;
}
My company just launched a new website with OS Ticket. This OS Ticket was attached to the old WordPress website before we make a switch. Currently, OS Ticket does not load with OS Ticket Content and navigation.
I want to successfully migrate OS ticket database to a new website, but I get these errors:
php error – /support/main.inc.php on line 78
php error – /support/include/class.nav.php on line 321
If it does not load, it means the path not found. I don’t know where to look for it. Any help is greatly appreciated.
[06-Sep-2016 09:09:27 America/Denver] PHP Warning: include(): Failed opening '/support/../wp-blog-header.php' for inclusion (include_path='./:/support/include/:/support/include/pear/') in /support/main.inc.php on line 78
<?php
/*********************************************************************
class.nav.php
Navigation helper classes. Pointless BUT helps keep navigation clean and free from errors.
Peter Rotich <peter#osticket.com>
Copyright (c) 2006-2013 osTicket
http://www.osticket.com
Released under the GNU General Public License WITHOUT ANY WARRANTY.
See LICENSE.TXT for details.
vim: expandtab sw=4 ts=4 sts=4:
**********************************************************************/
require_once(INCLUDE_DIR.'class.app.php');
class StaffNav {
var $activetab;
var $activeMenu;
var $panel;
var $staff;
function StaffNav($staff, $panel='staff'){
$this->staff=$staff;
$this->panel=strtolower($panel);
}
function __get($what) {
// Lazily initialize the tabbing system
switch($what) {
case 'tabs':
$this->tabs=$this->getTabs();
break;
case 'submenus':
$this->submenus=$this->getSubMenus();
break;
default:
throw new Exception($what . ': No such attribute');
}
return $this->{$what};
}
function getPanel(){
return $this->panel;
}
function isAdminPanel(){
return (!strcasecmp($this->getPanel(),'admin'));
}
function isStaffPanel() {
return (!$this->isAdminPanel());
}
function getRegisteredApps() {
return Application::getStaffApps();
}
function setTabActive($tab, $menu=''){
if($this->tabs[$tab]){
$this->tabs[$tab]['active']=true;
if($this->activetab && $this->activetab!=$tab && $this->tabs[$this->activetab])
$this->tabs[$this->activetab]['active']=false;
$this->activetab=$tab;
if($menu) $this->setActiveSubMenu($menu, $tab);
return true;
}
return false;
}
function setActiveTab($tab, $menu=''){
return $this->setTabActive($tab, $menu);
}
function getActiveTab(){
return $this->activetab;
}
function setActiveSubMenu($mid, $tab='') {
if(is_numeric($mid))
$this->activeMenu = $mid;
elseif($mid && $tab && ($subNav=$this->getSubNav($tab))) {
foreach($subNav as $k => $menu) {
if(strcasecmp($mid, $menu['href'])) continue;
$this->activeMenu = $k+1;
break;
}
}
}
function getActiveMenu() {
return $this->activeMenu;
}
function addSubMenu($item,$active=false){
// Triger lazy loading if submenus haven't been initialized
isset($this->submenus[$this->getPanel().'.'.$this->activetab]);
$this->submenus[$this->getPanel().'.'.$this->activetab][]=$item;
if($active)
$this->activeMenu=sizeof($this->submenus[$this->getPanel().'.'.$this->activetab]);
}
function getTabs(){
if(!$this->tabs) {
$this->tabs=array();
$this->tabs['dashboard'] = array('desc'=>__('Dashboard'),'href'=>'dashboard.php','title'=>__('Agent Dashboard'), "class"=>"no-pjax");
$this->tabs['users'] = array('desc' => __('Users'), 'href' => 'users.php', 'title' => __('User Directory'));
$this->tabs['tickets'] = array('desc'=>__('Tickets'),'href'=>'tickets.php','title'=>__('Ticket Queue'));
$this->tabs['kbase'] = array('desc'=>__('Knowledgebase'),'href'=>'kb.php','title'=>__('Knowledgebase'));
if (count($this->getRegisteredApps()))
$this->tabs['apps']=array('desc'=>__('Applications'),'href'=>'apps.php','title'=>__('Applications'));
}
return $this->tabs;
}
function getSubMenus(){ //Private.
global $cfg;
$staff = $this->staff;
$submenus=array();
foreach($this->getTabs() as $k=>$tab){
$subnav=array();
switch(strtolower($k)){
case 'tickets':
$subnav[]=array('desc'=>__('Tickets'),'href'=>'tickets.php','iconclass'=>'Ticket', 'droponly'=>true);
if($staff) {
if(($assigned=$staff->getNumAssignedTickets()))
$subnav[]=array('desc'=>__('My Tickets')." ($assigned)",
'href'=>'tickets.php?status=assigned',
'iconclass'=>'assignedTickets',
'droponly'=>true);
if($staff->canCreateTickets())
$subnav[]=array('desc'=>__('New Ticket'),
'title' => __('Open a New Ticket'),
'href'=>'tickets.php?a=open',
'iconclass'=>'newTicket',
'id' => 'new-ticket',
'droponly'=>true);
}
break;
case 'dashboard':
$subnav[]=array('desc'=>__('Dashboard'),'href'=>'dashboard.php','iconclass'=>'logs');
$subnav[]=array('desc'=>__('Agent Directory'),'href'=>'directory.php','iconclass'=>'teams');
$subnav[]=array('desc'=>__('My Profile'),'href'=>'profile.php','iconclass'=>'users');
break;
case 'users':
$subnav[] = array('desc' => __('User Directory'), 'href' => 'users.php', 'iconclass' => 'teams');
$subnav[] = array('desc' => __('Organizations'), 'href' => 'orgs.php', 'iconclass' => 'departments');
break;
case 'kbase':
$subnav[]=array('desc'=>__('FAQs'),'href'=>'kb.php', 'urls'=>array('faq.php'), 'iconclass'=>'kb');
if($staff) {
if($staff->canManageFAQ())
$subnav[]=array('desc'=>__('Categories'),'href'=>'categories.php','iconclass'=>'faq-categories');
if ($cfg->isCannedResponseEnabled() && $staff->canManageCannedResponses())
$subnav[]=array('desc'=>__('Canned Responses'),'href'=>'canned.php','iconclass'=>'canned');
}
break;
case 'apps':
foreach ($this->getRegisteredApps() as $app)
$subnav[] = $app;
break;
}
if($subnav)
$submenus[$this->getPanel().'.'.strtolower($k)]=$subnav;
}
return $submenus;
}
function getSubMenu($tab=null){
$tab=$tab?$tab:$this->activetab;
return $this->submenus[$this->getPanel().'.'.$tab];
}
function getSubNav($tab=null){
return $this->getSubMenu($tab);
}
}
class AdminNav extends StaffNav{
function AdminNav($staff){
parent::StaffNav($staff, 'admin');
}
function getRegisteredApps() {
return Application::getAdminApps();
}
function getTabs(){
if(!$this->tabs){
$tabs=array();
$tabs['dashboard']=array('desc'=>__('Dashboard'),'href'=>'logs.php','title'=>__('Admin Dashboard'));
$tabs['settings']=array('desc'=>__('Settings'),'href'=>'settings.php','title'=>__('System Settings'));
$tabs['manage']=array('desc'=>__('Manage'),'href'=>'helptopics.php','title'=>__('Manage Options'));
$tabs['emails']=array('desc'=>__('Emails'),'href'=>'emails.php','title'=>__('Email Settings'));
$tabs['staff']=array('desc'=>__('Agents'),'href'=>'staff.php','title'=>__('Manage Agents'));
if (count($this->getRegisteredApps()))
$tabs['apps']=array('desc'=>__('Applications'),'href'=>'apps.php','title'=>__('Applications'));
$this->tabs=$tabs;
}
return $this->tabs;
}
function getSubMenus(){
$submenus=array();
foreach($this->getTabs() as $k=>$tab){
$subnav=array();
switch(strtolower($k)){
case 'dashboard':
$subnav[]=array('desc'=>__('System Logs'),'href'=>'logs.php','iconclass'=>'logs');
$subnav[]=array('desc'=>__('Information'),'href'=>'system.php','iconclass'=>'preferences');
break;
case 'settings':
$subnav[]=array('desc'=>__('Company'),'href'=>'settings.php?t=pages','iconclass'=>'pages');
$subnav[]=array('desc'=>__('System'),'href'=>'settings.php?t=system','iconclass'=>'preferences');
$subnav[]=array('desc'=>__('Tickets'),'href'=>'settings.php?t=tickets','iconclass'=>'ticket-settings');
$subnav[]=array('desc'=>__('Emails'),'href'=>'settings.php?t=emails','iconclass'=>'email-settings');
$subnav[]=array('desc'=>__('Access'),'href'=>'settings.php?t=access','iconclass'=>'users');
$subnav[]=array('desc'=>__('Knowledgebase'),'href'=>'settings.php?t=kb','iconclass'=>'kb-settings');
$subnav[]=array('desc'=>__('Autoresponder'),'href'=>'settings.php?t=autoresp','iconclass'=>'email-autoresponders');
$subnav[]=array('desc'=>__('Alerts and Notices'),'href'=>'settings.php?t=alerts','iconclass'=>'alert-settings');
break;
case 'manage':
$subnav[]=array('desc'=>__('Help Topics'),'href'=>'helptopics.php','iconclass'=>'helpTopics');
$subnav[]=array('desc'=>__('Ticket Filters'),'href'=>'filters.php',
'title'=>__('Ticket Filters'),'iconclass'=>'ticketFilters');
$subnav[]=array('desc'=>__('SLA Plans'),'href'=>'slas.php','iconclass'=>'sla');
$subnav[]=array('desc'=>__('API Keys'),'href'=>'apikeys.php','iconclass'=>'api');
$subnav[]=array('desc'=>__('Pages'), 'href'=>'pages.php','title'=>'Pages','iconclass'=>'pages');
$subnav[]=array('desc'=>__('Forms'),'href'=>'forms.php','iconclass'=>'forms');
$subnav[]=array('desc'=>__('Lists'),'href'=>'lists.php','iconclass'=>'lists');
$subnav[]=array('desc'=>__('Plugins'),'href'=>'plugins.php','iconclass'=>'api');
break;
case 'emails':
$subnav[]=array('desc'=>__('Emails'),'href'=>'emails.php', 'title'=>__('Email Addresses'), 'iconclass'=>'emailSettings');
$subnav[]=array('desc'=>__('Banlist'),'href'=>'banlist.php',
'title'=>__('Banned Emails'),'iconclass'=>'emailDiagnostic');
$subnav[]=array('desc'=>__('Templates'),'href'=>'templates.php','title'=>__('Email Templates'),'iconclass'=>'emailTemplates');
$subnav[]=array('desc'=>__('Diagnostic'),'href'=>'emailtest.php', 'title'=>__('Email Diagnostic'), 'iconclass'=>'emailDiagnostic');
break;
case 'staff':
$subnav[]=array('desc'=>__('Agents'),'href'=>'staff.php','iconclass'=>'users');
$subnav[]=array('desc'=>__('Teams'),'href'=>'teams.php','iconclass'=>'teams');
$subnav[]=array('desc'=>__('Groups'),'href'=>'groups.php','iconclass'=>'groups');
$subnav[]=array('desc'=>__('Departments'),'href'=>'departments.php','iconclass'=>'departments');
break;
case 'apps':
foreach ($this->getRegisteredApps() as $app)
$subnav[] = $app;
break;
}
if($subnav)
$submenus[$this->getPanel().'.'.strtolower($k)]=$subnav;
}
return $submenus;
}
}
class UserNav {
var $navs=array();
var $activenav;
var $user;
function UserNav($user=null, $active=''){
$this->user=$user;
$this->navs=$this->getNavs();
if($active)
$this->setActiveNav($active);
}
function getRegisteredApps() {
return Application::getClientApps();
}
function setActiveNav($nav){
if($nav && $this->navs[$nav]){
$this->navs[$nav]['active']=true;
if($this->activenav && $this->activenav!=$nav && $this->navs[$this->activenav])
$this->navs[$this->activenav]['active']=false;
$this->activenav=$nav;
return true;
}
return false;
}
function getNavLinks(){
global $cfg;
//Paths are based on the root dir.
if(!$this->navs){
$navs = array();
$user = $this->user;
$navs['home']=array('desc'=>__('Support Center Home'),'href'=>'index.php','title'=>'');
if($cfg && $cfg->isKnowledgebaseEnabled())
$navs['kb']=array('desc'=>__('Knowledgebase'),'href'=>'kb/index.php','title'=>'');
// Show the "Open New Ticket" link unless BOTH client
// registration is disabled and client login is required for new
// tickets. In such a case, creating a ticket would not be
// possible for web clients.
if ($cfg->getClientRegistrationMode() != 'disabled'
|| !$cfg->isClientLoginRequired())
$navs['new']=array('desc'=>__('Open a New Ticket'),'href'=>'open.php','title'=>'');
if($user && $user->isValid()) {
if(!$user->isGuest()) {
$navs['tickets']=array('desc'=>sprintf(__('Tickets (%d)'),$user->getNumTickets()),
'href'=>'tickets.php',
'title'=>__('Show all tickets'));
} else {
$navs['tickets']=array('desc'=>__('View Ticket Thread'),
'href'=>sprintf('tickets.php?id=%d',$user->getTicketId()),
'title'=>__('View ticket status'));
}
} else {
$navs['status']=array('desc'=>__('Check Ticket Status'),'href'=>'view.php','title'=>'');
}
$this->navs=$navs;
}
return $this->navs;
}
function getNavs(){
return $this->getNavLinks();
}
}
?>
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.
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.