This is Drupal 6.x and am having a nightmare of a time to modify a simple drupal form. This is a module file.
function modulename_menu() {
$items = array();
$items['school/registration'] = array(
'title' => 'Registration Form',
'page callback' =>'drupal_get_form',
'type' => MENU_CALLBACK
);
return $items;
}//end of the function
function modulename_school_form_alter(&$form, $form_state, $form_id)
{
// dsm($form_id);
if ($form_id == 'user_registration_form')
{
// modify the "#submit" form property by prepending another submit handler arra
$form['#submit'] = array_merge(
array('_modulename_registration_submit' => array()),
$form['#submit']
);
}
}
For what it's worth a few months afterwards - I just had the same problem. Check out
http://drupal.org/node/626834#comment-2393090
You probably have an install file with your custom module that doesn't contain all the necessary information.
Related
I am trying to figure out a way (if possible) to remove or hide certain fields in a custom tab. The custom tab is labeled "Rotator" and it holds images that can be used for a rotating banner on a page. The home page banner is a little different in that it has 2 extra fields that aren't needed on the subpages: BackgroundImage and Body(which is meant to hold a variety of text). I want to make things simple for the content manager, so I want to hide these fields on the subpages.
I am aware of removeFieldFromTab and how it works, and I was thinking of using it on the Page.php file (since that is basically the main template for all page types in my SilverStripe file):
public function getCMSFields() {
$fields = parent::getCMSFields();
$gridFieldConfig = GridFieldConfig_RecordEditor::create();
$gridFieldConfig->addComponent(new GridFieldBulkImageUpload());
$gridFieldConfig->addComponent(new GridFieldSortableRows('SortOrder'));
$gridFieldConfig->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
// field from drawer class => label in UI
'ID' => 'ID',
'Title' => 'Title',
'Thumbnail' => 'Thumbnail',
'InternalURL.Link' => 'Internal URL',
));
$gridfield = new GridField(
"Rotator",
"Rotator",
$this->Rotator()->sort("SortOrder"),
$gridFieldConfig
);
$fields->addFieldToTab('Root.Rotator', $gridfield);
$fields->addFieldToTab("Root.Main", new TextField("H1"), "Content");
$fields->addFieldToTab("Root.Main", new TextField("Subheader"), "Content");
$fields->addFieldToTab('Root.Main', new TextField('PageTitle', 'Page Title'), 'MetaDescription');
$fields->removeFieldFromTab('Root.Rotator', 'Body');
$fields->removeFieldFromTab('Root.Rotator', 'BackgroundImage');
return $fields;
}
Here is the code for the Rotator class:
<?php
class RotatorImage extends DataObject {
public static $db = array(
'SortOrder' => 'Int',
'Header' => 'varchar',
'Body' => 'HTMLText',
);
// One-to-one relationship with gallery page
public static $has_one = array(
'Image' => 'Image',
'BackgroundImage' => 'Image',
'Page' => 'Page',
'InternalURL' => 'SiteTree',
);
// tidy up the CMS by not showing these fields
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->removeFieldFromTab("Root.Main","PageID");
$fields->removeFieldFromTab("Root.Main","SortOrder");
return $fields;
}
// Tell the datagrid what fields to show in the table
public static $summary_fields = array(
'ID' => 'ID',
'Title' => 'Title',
'Thumbnail' => 'Thumbnail',
'InternalURLID' => 'Internal URL',
);
// this function creates the thumnail for the summary fields to use
public function getThumbnail() {
return $this->Image()->CMSThumbnail();
}
public function canEdit() {
return true;
}
public function canDelete() {
return true;
}
public function canCreate(){
return true;
}
public function canPublish(){
return true;
}
public function canView(){
return true;
}
}
However this does not work, and I am sure that I have the fields names correct. I tried 'Root.Rotator.Main' and 'Root.Rotator.Content' just to see what would happen and those also did not work. What am I missing? Is it possible to hide fields on a custom tab this way, or do I need to try something else?
well, you want to hide the fields in the gridfield detail form? that cannot be done in your pages getCMSFields(), as the grid is responsible for generating the detail form. Two possible solutions:
1) tell the grid to hide that fields with a custom component. I dunno how to do it
2) tell your Rotator class to show the fields ONLY if the related page is a homepage:
public function getCMSFields() {
$fields = parent::getCMSFields();
//...other stuff....
$isOnHomePage = ($this->Page() && $this->Page()->ClassName == 'HomePage'); //put in your own classname or conditions
if(!$isOnHomePage) {
//remove the fields if you're not on the HomePage
$fields->removeByName('Body');
//we need to suffix with "ID" when we have a has_one relation!
$fields->removeByName('BackGroundImageID');
}
return $fields;
}
This will work...
$fields->removeByName('FieldName');
I want to change the view of the module depending on the Url. All in php.
I created two views putting this data:
function config_services_block_info() {
$blocks['config_services'] = array(
// The name that will appear in the block list.
'info' => t('Services'),
// Default setting.
'cache' => DRUPAL_CACHE_PER_ROLE, );
$blocks['orderservices'] = array(
// The name that will appear in the block list.
'info' => t('Order Services'),
// Default setting.
'cache' => DRUPAL_CACHE_PER_ROLE, );
return $blocks;
}
function config_services_block_view($delta = '') { switch ($delta) {
case 'config_services':
...
block[content] = ...;
return block;
break;
case 'orderservices':
...
block[content] = ...;
return block;
break; } }
function config_services_menu() { $items = array();
$items['config_services/orderservices'] = array(
'title' => t('Order Services'),
'page callback' => array('_config_services_orderservices_page'),
'access arguments' => array('order config_services content'),
'type' => MENU_CALLBACK, //Will appear in block. ); return $items; }
In _config_services_orderservices_page() I think this but no work:
function _config_services_orderservices_page() {
config_services_block_view('orderservices');
}
The first view works, the problem is when I want the second view. How I change the view for the url: http:(slash)(slash)name-web/config_services/orderservices
Depending on the condition you want to change the view, the condition could be checked inside _config_services_orderservices_page() function, and specific block could be displayed.
function _config_services_orderservices_page() {
if (your_condition == 'orderservices') {
config_services_block_view('orderservices');
}
if (other_condition == 'config_services') {
config_services_block_view('config_services');
}
}
On a side note, view in Drupal means something totally different. It is the most popular and most used module overall. Please visit the project page, and the documentation.
And what you refer to is about blocks.
I have created a module in SocialEngine(*which is built on Zend framework v1.9) that contains an admin form with a few options.
The problem I have with it is that it seems to no get the values of the fields from database after I refresh the page and it shows me the default values.
It shows the correct values immediately after I save(*but I am not sure if the page is refreshed after saving), but not after I refresh.
controller /application/modules/Mymodule/controllers/AdminSomesettingsController.php :
class Mymodule_AdminSomesettingsController extends Core_Controller_Action_Admin
{
public function indexAction()
{
$this->view->form = $form = new Mymodule_Form_Admin_Someform();
$settings = Engine_Api::_()->getApi('settings', 'core');
if(!$form->isValid($settings->mymodule))
{ return $form->populate($settings->mymodule); }
if( !$this->getRequest()->isPost() ) { return; }
if( !$form->isValid($this->getRequest()->getPost()) ) { return; }
$db = Engine_Api::_()->getDbTable('settings','core')->getAdapter();
$db->beginTransaction();
try {
$values = $form->getValues();
$settings->mymodule = $values;
$db->commit();
} catch( Exception $e ) {
$db->rollback();
throw $e;
}
$form->saveValues();
$form->addNotice('Your changes have been saved.');
}
}
form /application/modules/Mymodule/Form/Admin/Someform.php :
class Mymodule_Form_Admin_Someform extends Engine_Form
{
public function init()
{
$this
->setTitle('My Settings')
->setDescription('Settings');
$this->addElement('Radio', 'some_setting', array(
'label' => 'Some Setting',
'description' => '',
'multiOptions' => array(
0 => 'Option One',
1 => 'Option Two',
2 => 'Option Three',
),
'value' => 1,
'escape' => false,
));
// Add submit button
$this->addElement('Button', 'submit', array(
'label' => 'Save Changes',
'type' => 'submit',
'ignore' => true
));
}
public function saveValues()
{
}
}
I have checked with other plugins and it seems to me that $form->populate($settings->mymodule); repopulates the form after refresh, but it does not work for me.
Any idea how I could make it show the values from the database(*when these values exist) instead of the default values?
I myself am new to socialengine and zend.My understanding of socialengine says, make a function saveValues() inside ur form class, then call it from controller action as $form->saveValues(),passing parameter as needed.This is the convention that socialengine seems to follow, and inside the saveValues() of form class,u can save valus as needed.Ur form shud be populated only if validation fails
(!$form->isValid($formData ))
{ return $form->populate($formData); }
Instead of default adapter,U should try this-
$db =Engine_Api::_()->getDbTable('settings','core')->getAdapter(),
$db->beginTransaction();
If u want to set the value of a particular field try - $form->populate(array('formField'=>'urValue')); in ur case maybe -
$val=$settings->mymodule,
$form->populate('formField'=>$val);
You can add the code in controller $form->some_setting->setValue('1');
I've tried the simplest examples of using hook_menu() posted here and on other Drupal forms and noting seems to work. My code, in: /sites/themes/mytheme/mymodule.module, is as follows:
<?php
function helloworld_menu() {
$items = array();
$items['hello'] = array(
'title' => 'Hello world!',
'type' => MENU_CALLBACK,
'page callback' => 'helloworld_page',
'access callback' => TRUE,
);
return $items;
}
function helloworld_page() {
return 'Hello world !';
}
When I navigate to www.mydomain.com/hello I get a 404 error. I've tried enabling and disabling the module along with clearing the cache numerous times with no luck still. Here is some additional information about my environment:
Running Drupal Commerce version 7.22
I've enabled clean URLS and pathauto module
The end goal I'm trying to achieve is adding products to the cart with a link. I already have that part working so that I can pass product ID's into a function and add them to cart. I would be replacing helloworld_page() with my function and then changing $items['hello'] to $items['cart/add/%/%'], having two wildcards (product ID and quantity).
For a hook declaration like hook_menu, the function name should be like <your_module_name_here>_menu
This is where you are going wrong.
Your module name is mymodule.module so your hook_menu should be called, mymodule_menu
<?php
/**
* Implements hook_menu().
*/
function mymodule_menu() {
$items = array();
$items['hello'] = array(
'title' => 'Hello world!',
'type' => MENU_CALLBACK,
'page callback' => 'helloworld_page',
'access callback' => TRUE,
);
return $items;
}
function helloworld_page() {
return 'Hello world !';
}
Please correct the function name and clear your cache and try again.
ALso i noticed you put the module in an unconventional location.
Please move it to /sites/all/modules/custom/mymodule folder ( both mymodule.info and mymodule.module files ).
I'm having issues customizing a radio button for a specific form using hook_theme. Below is the piece of code I have on my module; see my comments elaborating the issue I encounter:
// Implementation of hook_form_alter().
function mymodule_form_alter(&$form, $form_state, $form_id){
// e.g form id: commerce_cart_add_to_cart_form_u6onPJSgS7pOgw0Tlo7zHy42LTQzbV913taANkYQKTo
if (strpos($form_id, 'commerce_cart_add_to_cart_form') !== FALSE) {
// Alter add to cart form
mymodule_commerce_cart_add_to_cart_form_alter($form, $form_state, $form_id);
}
}
function mymodule_commerce_cart_add_to_cart_form_alter(&$form, $form_state, $form_id) {
// Change the field type to radios.
$form['attributes']['field_artwork_ref']['#type'] = 'radios';
// Apply my custom theme for radios.
$form['attributes']['field_artwork_ref']['#theme'] = array('custom_radios');
}
// Implementation of hook_theme().
function mymodule_theme() {
return array(
'custom_radios' => array(
'variables' => array('element' => NULL),
),
);
}
function theme_custom_radios($variables) {
// Custom theme should go here.
// However, $variables are empty, print_r gives me "Array ( [element] => )."
// I am at least expecting to see my radio element here.
print_r($variables);
}
Themes for Drupal 7 form elements need to use the new render array key instead of variables in the theme definition:
function mymodule_theme() {
return array(
'custom_radios' => array(
'render element' => 'element',
),
);
}
Once you've made the change clear Drupal's caches and your code should work (I've just tested the above and it works fine).