How to add a custom Rules "action" in Drupal? - drupal

I'm using the Rules module in Drupal 7, and I tried to add a new Rules "Action"
I followed the steps described in How to create an custom rule action using hook_rules_action_info? to create a custom rule action using hook_rules_action_info:
I tried to create a sample file (save_nid.rules.inc) in the folder /rules/module/.
And I also tried to create a module folder in /site/all/module/save_nid/.
My code in save_nid.rules.inc looks like so:
function rules_save_nid_action_info() {
return array(
'save_nid_action' => array(
'label' => t('Custom Action'),
'parameter' => array(
'param1' => array(
'type' => 'int',
'label' => t('Parameter Label'),
),
'param2' => array(
'type' => 'commerce_order',
'label' => t('Parameter Label2'),
),
),
'configurable' => FALSE,
'group' => t('ABC Custom module action'),
'callbacks' => array(
'execute' => 'abc_custom_action',
),
),
}
After I cleared the Drupal cache, I didn't see my "custom rule" in the list.
What am I missing or doing wrong?

In your case, the machine name of your custom module seems to be save_nid.
So if you want to use hook_rules_action_info in your module, your function needs to be named save_nid_rules_action_info, instead of rules_save_nid_action_info.
Not sure if it is the "only" problem why you can't get it to work, but at least it is "a" problem you need to address.
PS: make sure to save that custom coding in your custom module's folder (in /site/all/module/save_nid/).

Related

Change post-type rewrite previously defined

My site includes a third party plugin which includes the below:
register_post_type( 'portfolio',
...
'rewrite' => array( 'slug' => 'portfolio' )
...
)
However for my site I need it to effectively say
...
'rewrite' => array( 'with_front' => false, 'slug' => 'portfolio' )
...
However I don't want to edit this third party plugin and cause problems for future updating. Is it possible to define the extra argument in my own plugin or theme (I don't think a theme can change rewrite)?
For the benefit of anyone else, the solution I've arrived at after another frustrating day working on this was to create a new plugin which read in all the existing plugin details via
get_post_types(array('name' => 'portfolio'),'objects');
edit the rewrite
$rebuiltportfolio["rewrite"]=array('with_front' => false, 'slug' => 'portfolio');
and then re-registered the post type
register_post_type( 'portfolio', $rebuiltportfolio);
You do need to ensure that this action has a higher priority than the original one.

Creating custom options for "List-item" option in Option Tree plugin for wordpress

I have a problem, I need to change the default options for a “List-item” option in Option tree . Right now the default are “Title/Image/Link/Descriptions” .. I want to remove them and add my own. I have written this code:
array(
'id' => 'academic_success_content',
'label' => 'Academic Success Content',
'desc' => 'Enter the academic success content. It will appear in the home page in list items format',
'std' => '',
'type' => 'list-item',
'section' => 'academic_perfomance',
'settings' => array(
array(
'id' => 'academic_success',
'label' => 'Academic Success',
'type' => 'textarea-simple',
)
)
),
But when I preview the themes options , the default list item "title" is still there and I only want to see the Academic Success textarea. What should I do?
I also suffered from the same situation.
And after that, I use the older version of "list-item" and "gallery".
This has worked well. Ex: v2.4.6
Is the theme options page being included?
You'll need to reference it in your functions.php, something to the effect of:
require_once locate_template('/path-to-your/theme-options.php' );

Drupal 7 - how to allow file extensions for field

After creating multiple fields with the help of the installation profile I am now also trying to figure out how I can add the allowed file extensions in the installation profile. Here is my code so far:
'name' => 'attachment',
'type' => 'file',
'settings' => array(
'allowed_file_extensions' => array('txt pdf'),
),
The allowed_file_extensions was just a test to see if it works but it doesn't it gives an fatal PHP error. (** PHP Fatal error: Unsupported operand types** )
How can I add the file extensions at the field?
PS: I also tried putting the settings in the instance, this gives the same error.
Oke, I was having this problem for the last three days. Now after a few hours after I posted this question I solved the problem.
For those who have the same question or problem here is what solved it for me and what the right way is to add settings. The settings can be different for each instance so the settings go at the instance creation and not the field itself.
Here is an example on how it is supposed to be:
$instance = array(
'field_name' => 'file'
'entity_type' => 'node',
'bundle' => 'article',
'label' => 'Attachment',
'description' => 'Add an attachment here',
'required' => TRUE,
'settings' => array(
'max_filesize' => '512',
'file_extensions' => 'zip txt pdf docx'
),
);
field_create_instance($instance);
The settings field is not required but i was using an foreach because I generate multiple fields and instances at once, if you do this and you have settings at 1 of your generated field instances then you have to add settings at all instances or check if it is there or not.
I hope my experience can help any of you out when having the same problem.

Drupal7 .install script not working

I am trying to convert my module from drupal6 to drupal 7 this is my code. The database table is not created.
function example_install() {
drupal_install_schema('example');
}
/**
* Implements hook_schema().
*/
function example_schema() {
$schema['example'] = array(
'description' => 'example settings',
'fields' => array(
'name' => array(
'description' => 'name',
'type' => 'varchar',
'length' => '255',
'not null' => TRUE,
),
'age' => array(
'description' => 'age',
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
),
) ,
);
return $schema;
}
Can any one explain whats wrong.
You don't want to run drupal_install_schema() yourself in Drupal 7, hook_schema() is called automatically if it exists in the .install file. That will probably cause a few problems but you'd still expect the table to be created at least once.
Once you've removed hook_install() try uninstalling (not just disabling) your module, then re-enabling it. I recommend the Devel module to do this as it provides a page (devel/reinstall) where you can easily force a reinstall of a module.
If you don't want to do that though, go to the modules page, disable the module, then click the 'Uninstall' tab at the top to uninstall it fully. Then go back to the modules page and re-enable it.
Doing this should force Drupal to re-run your hook_schema() script and the table should be created.

Managing several custom content types from one module(drupal)

Is it possible to declare and manage several custom content types inside one module? I'm creating a site that needs four custom content types and I'd like to manage them from one module instead of creating module for every content type. After some testing, I found out that it seems impossible. Because, unless hook_form and content type share the same name of module, drupal doesn't call hook_form.
Here's how I'd like to do -
function mycontent_node_info(){
return array(
'mycontent1' => array(
'name' => t('....'),
'module' => 'mycontent',
'description' => t('...),
'has_title' => TRUE,
'title_label' => t('Title'),
'has_body' => TRUE,
'body_label' => t('content body'),
),
'mycontent2' => array(
.......
),
'mycontent3' => array(
......
),
'mycontent4' => array(
......
),
);
}
function mycontent1_form(&$node){
$form['control1'] = array(
'#type' => 'select',
'#options' => array(
'0' => t('selection 1'),
'1' => t('selection 2'),
),
'#attributes' => array('id'=>'control1'),
);
$form['control2'] = array(
'#type' => 'select',
'#options' => array(
'0' => t('1'),
'1' => t('2'),
'2' => t('3'),
'3' => t('4'),
),
'#attributes' => array('id'=>'control2'),
);
return $form;
}
function mycontent2_form(&$node){
....
}
function mycontent3_form(&$node){
....
}
function mycontent4_form(&$node){
....
}
Am I doing something wrong here or is not possible and there's no alternative other than creating module for every content types. I appreciate much your help.
The prefix for all your hooks should be the name of your module, i.e. mycontent_node_info() and mycontent_form(&$node). I think the content type itself can be called whatever you want but by convention anything global that you define in a module should be prefixed with the name of the module to avoid namespace problems. So your content becomes mycontent_type1, mycontent_type2, etc... As for dealing with hook_form, I guess the way to do it is to check the type of the node passed in and act accordingly.
You might try using the Features module (http://drupal.org/project/features) to export your content types. It auto generates the code to make this work, and you can have a look at what is going wrong with your code.

Resources