Adding html class/ID to hook_menu generated link - php

I'm trying to add a class or ID to a link generated from my hook_menu code included below:
<?php
function mymodule_menu() {
$items['mypage_redirect'] = array(
'page callback' => 'mypage_redirect_callback',
'type' => MENU_CALLBACK,
'access arguments' => array("access content"),
'title' => 'My module generated page',
'options' => array(
'attributes' => array(
'class' => array(
'test-class'
)
)
),
);
return $items;
}
All I'm trying to accomplish a simple link with a class of 'test-class' so I can style it but the instructions I followed to get the above code has not worked.

function commons_menu() {
$items = array();
$items['test'] = array(
'title' => t("test"),
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM,
'options' => array(
'attributes' => array(
'class' => 'test-menu'
)
)
);
return $items;
}
Have cheked other forums too!!.. the attibutes doesn't work with MENU_LOCAL_ACTION or MENU_LOCAL_TASK
Your code looks fine.. did you try clearing cache after the changes?

Related

Drupal 7: how do I save module settings?

I am trying to create some settings for a module and am following the guide on Drupal.org. I am also comparing my work to existing modules.
The config menu appears in the correct place with the correct fields but when I hit save no input is saved. (I have run cache clear and registry rebuild.)
Would anyone know what I am doing wrong here? I cannot see how my stuff differs.
On my .admin.inc file I have set the form as such:
function contact_page_settings() {
$form = array();
$config = contact_page_default_settings();
$form['#tree'] = TRUE;
$form['contact_page_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Top Section'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'top-title' => array(
'#type' => 'textfield',
'#title' => t('Top title'),
'#default_value' => !empty($config['top-title']) ? $config['top-title'] : '',
),
'top-left' => array(
'#type' => 'text_format',
'#title' => t('Top left'),
'#default_value' => !empty($config['top-left']) ? $config['top-left'] : '',
),
'top-right' => array(
'#type' => 'text_format',
'#title' => t('Top right'),
'#default_value' => !empty($config['top-right']) ? $config['top-right'] : '',
),
);
return system_settings_form($form);
}
On my .module file I have:
function contact_page_menu() {
$items = array();
$items['admin/settings/contact-page'] = array(
'title' => 'Contact Page content',
'page callback' => 'drupal_get_form',
'access arguments' => array('administer site configuration'),
'page arguments' => array('contact_page_settings'),
'type' => MENU_NORMAL_ITEM,
'file' => '/admin/contact_page.admin.inc',
);
return $items;
}
function contact_page_default_settings() {
$defaults = array(
'top-tile' => 'Top title',
'top-left' => 'Top left',
'top-right' => 'Top right',
);
$config = variable_get('contact_page_settings', array());
return array_merge($defaults, $config);
}
OK, so I found out the values were saving - just they were not appearing in my text areas when I reloaded the config page.
The problem was that I was using text_format fields which save data as an array. I needed to get the value property of that array as my default value.
So, I just added ['value'] to the default_value ternary expression:
'#default_value' => !empty($config['top-right']['value']) ? $config['top-right']['value'] : '',

Drupal: Edit/delete is not working on the custom module

I am a newbie to drupal. I am working to create a custom module in the name "course". I have read about the list of hooks to create a custom module. So, what's my problem is that i cant able to see the edit form for the module configure but i can do add management and its working fine.
I've used Existing contact modules as a reference.
Below are my code:
In course.module
function course_menu() {
$items['admin/structure/course'] = array(
'title' => 'Academy\'s courses',
'description' => 'Create a system contact form and set up categories for the form to use.',
'page callback' => 'course_list',
'access arguments' => array('administer contact forms'),
'file' => 'course.admin.inc',
);
$items['admin/structure/course/add'] = array(
'title' => 'Add Courses',
'page callback' => 'drupal_get_form',
'page arguments' => array('course_edit_form'),
'access arguments' => array('administer contact forms'),
'type' => MENU_LOCAL_ACTION,
'weight' => 1,
'file' => 'course.admin.inc',
);
$items['admin/structure/course/edit/%course'] = array(
'title' => 'Edit Courses',
'page callback' => 'drupal_get_form',
'page arguments' => array('course_edit_form',4),
'access arguments' => array('administer contact forms'),
'file' => 'course.admin.inc',
);
return $items;
}
and in course.admin.inc:
function course_edit_form($form, &$form_state, array $course = array()) {
// If this is a new course, add the default values.
$course += array(
'name' => '',
'course_id' => NULL,
);
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Course Name'),
'#maxlength' => 255,
'#default_value' => $course['name'],
'#description' => t("Example: 'Available Course Names'."),
'#required' => TRUE,
);
$form['course_id'] = array(
'#type' => 'value',
'#value' => $course['course_id'],
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
function course_edit_form_submit($form, &$form_state) {
if (empty($form_state['values']['course_id'])) {
db_insert('courses') // Table name no longer needs {}
->fields(array(
'name' => $form_state['values']['name'],
'course_url' => $form_state['values']['name']
))
->execute();
//drupal_write_record('courses', $form_state['values']);
}
else {
db_insert('courses') // Table name no longer needs {}
->fields(array(
'name' => $form_state['values']['name'],
'course_url' => $form_state['values']['name']
))
->condition('course_id', $form_state['values']['course_id'])
->execute();
//drupal_write_record('courses', $form_state['values'], array('course_id'));
}
drupal_set_message(t('Course %name has been saved.', array('%name' => $form_state['values']['name'])));
watchdog('contact', 'Course %name has been saved.', array('%course' => $form_state['values']['name']), WATCHDOG_NOTICE, l(t('Edit'), 'admin/structure/course/edit/' . $form_state['values']['course_id']));
$form_state['redirect'] = 'admin/structure/course';
}
I can't able to find the exact problem, why add is working but not edit & delete? Help me on this.
Below image tells you what exactly i am asking for(Red mark).
If anything additional needed in this please let me know.
At A guess I'd say the edit route is not matching because of an issue with the course load function (not listed). Try dumping the result of that method. Is the course entity defined in your module? If so, then the convention is to prefix the variable name with the module name, and implement the appropriately names hook.
There's a better description of how this should work here: https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_menu/7
Jump to the "Auto-Loader Wildcards" section.
Try to clear cache /admin/config/development/performance

Drupal 6 page and form custom module

I'd like to make a drupal page with a form. Something like the following, which doesn't render:
function score_table_menu() {
$items['table'] = array(
'title' => t('name'),
'page arguments' => array('table_page'),
'page arguments' => array('table_form'),
'description' => t('score table'),
'type' => MENU_CALLBACK,
);
return $items;
}
function table_page(){
$output .= t('Complicated Hello');
$header = stuff;
$rows = stuff;
$output .= theme_table($header, $rows);
return $output;
}
function table_form(){
$stuff_array = array (values);
$form['choice']= array(
'#type' => 'select',
'#title' => t('Select Stuff'),
'#options' => $stuff_array,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
function table_form_validate(){}
function table_form_submit(){
drupal_set_message(t('Submitted'));
}
Is it possible to have half the module render a page and the other half a form? I've written out two page arguments. I don't see any difference between page argument and callback. Also might need to use drupal_get_form() to render the form on the page.
page callback
is the function that you want this URI to trigger.
page arguments
are any arguments that you want to sent to that function when the callback is called.
Your menu item should look like this:
$items['table'] = array(
'title' => t('name'),
'page callback' => 'table_page',
'description' => t('score table'),
'type' => MENU_CALLBACK,
);
Or this:
$items['table'] = array(
'title' => t('name'),
'page callback' => 'drupal_get_form',
'page arguments' => array('table_form'),
'description' => t('score table'),
'type' => MENU_CALLBACK,
);
In your table_form, you can use other form types to display markup,
$form['table'] = array(
'#type' => 'markup',
'#markup' => theme_table($header, $rows),
);
Take a look in the API for a better understanding of hook_menu: http://api.drupal.org/api/drupal/developer%21hooks%21core.php/function/hook_menu/6

Associating an item to multiple other items (of a different class) using Prestashop's backoffice

Having just arrived at Prestashop 1.5, I am making a very simple module: a video of the week, associated with multiple products that need to appear right next to it.
I decided to start from the Backoffice. Right now, I can view, add, edit and remove all the Video entries but I'm a bit lost on how to map the N-N association between a video and its related products... The lack of documentation isn't helping either.
Any ideas how to pull this off?
Here's a bit of my code, the Video class is defined by:
class Video extends ObjectModel {
public $id_video;
public $title;
public $url;
public $active;
public static $definition = array(
'table' => 'video',
'primary' => 'id_video',
'multilang' => false,
'fields' => array(
'id_video' => array(
'type' => ObjectModel :: TYPE_INT
),
'title' => array(
'type' => ObjectModel :: TYPE_STRING,
'required' => true
),
'url' => array(
'type' => ObjectModel :: TYPE_STRING,
'required' => true
),
'active' => array(
'type' => ObjectModel :: TYPE_BOOL,
'required' => true
)
),
);
(...)
and the AdminVideo class is here:
class AdminVideoController extends ModuleAdminController {
public function __construct()
{
$this->table = 'video';
$this->className = 'Video';
$this->lang = false;
$this->fields_list['id_video'] = array(
'title' => $this->l('ID'),
'align' => 'center',
);
$this->fields_list['title'] = array(
'title' => $this->l('Title'),
'width' => 'auto'
);
$this->fields_list['url'] = array(
'title' => $this->l('URL'),
'width' => 'auto'
);
$this->fields_list['active'] = array(
'title' => $this->l('Active'),
'width' => '70',
'align' => 'center',
'active' => 'status',
'type' => 'bool',
'orderby' => false
);
parent::__construct();
}
public function postProcess()
{
parent::postProcess();
}
public function renderList()
{
$this->addRowAction('edit');
$this->addRowAction('delete');
$this->addRowAction('details');
return parent::renderList();
}
public function renderForm()
{
if (!($obj = $this->loadObject(true)))
return;
$this->fields_form = array(
'legend' => array(
'title' => $this->l('This weeks video'),
'image' => '../img/admin/world.gif'
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('Nome'),
'name' => 'title',
'size' => 33,
'required' => true,
'desc' => $this->l('Title')
),
array(
'type' => 'text',
'label' => $this->l('URL'),
'name' => 'url',
'size' => 33,
'required' => true,
'desc' => $this->l('Video URL')
),
array(
'type' => 'radio',
'label' => $this->l('Active:'),
'name' => 'active',
'required' => false,
'class' => 't',
'is_bool' => true,
'values' => array(
array(
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Disabled')
)
),
'desc' => $this->l('Only one video can be active at any given time')
),
)
);
if (Shop::isFeatureActive())
{
$this->fields_form['input'][] = array(
'type' => 'shop',
'label' => $this->l('Shop association:'),
'name' => 'checkBoxShopAsso',
);
}
$this->fields_form['submit'] = array(
'title' => $this->l(' Save '),
'class' => 'button'
);
if (!($obj = $this->loadObject(true)))
return;
return parent::renderForm();
}
}
One other thing: would it be possible to add a preview of the video inside the backoffice? I tried to echo YouTube's embed code, but it gets inserted even before the header. Is there a clean way of doing this or do I have to use some jQuery trickery? I was basically doing an echo of YT's embed code just before the end of postProcess().
Thanks in advance!
The simplest way to associate the videos to the products is by adding a "products" text field in your "video" table to store a comma separated list of the ids of the associated products (eg.: 1,10,27). Even if it's a bit rudimentary, it should work.
Alternatively, you could use a table like this:
create table video_product (
id_association int not null auto_increment,
id_video int,
id_product int,
primary key (id_association)
);
The problem with this solution is that the PrestaShop ObjectModel core does not provide any method to automatically update or delete the related tables (at least as far as I know), so you have to insert the code to manage the "video_product" table in your "Video" class.
If you want an example of how to do this, you should look at the classes/Product.php script, which manages the product table and all its related tables (categories, tags, features, attachments, etc.).
To have an idea of how the Prestashop database is structured, have a look at the docs/dbmodel.mwb file, which contains the schema of the database; this file can be viewed by using the MySQL Workbench application.

ahah not working in drupal 6

I defined in this my menu in D6. I want to enter text field value on clicking on checkbox.
function mymodule_menu(){
return array(
'assignJob/js'=>array(
'page callback' => 'assignJob_js',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
)
}
function assignJob_assignee() {
$output='xxxxxxx';
drupal_json(array('status' => TRUE, 'data' => $output));
}
While making form, there is checkbox called assignJob
$form[$group]['assignJob'] = array(
'#type' => 'checkbox',
'#title' => 'Assign Job',
'#ahah' => array(
'event' => 'change',
'path' => 'assignJob/js',
'wrapper' => 'edit-name',
'method' => 'append',
'effect' => 'none',
'progress' => array(
'type' => 'throbber',
),
)
Wrapper is the id of text field
When i click on this chcekbox i am getting
An HTTP error 404 occurred.
/assignJob/js
Can anybody help me what am i doing wrong here?
it seems like the path /assignJob/js doesn't exist.
make sure you don't have a typo when you declared it in hook_menu
and make sure you flushed all the caches when you change hook_menu

Categories