Drupal 7 - how to allow file extensions for field - drupal

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.

Related

ACF form file upload not working for non-admin users

I have an acf_form() on my website that includes some file upload fields. When I am logged in as admin, clicking the file upload button shows the wp media loader, when I am logged in as the custom role "user", I get the standard browser file upload and when the form is submitted, the file is not uploaded and does not appear in the media library.
Here is my acf_form() function:
acf_form(
array(
'id' => 'client-portal-acf-form',
'post_id' => 'new_post',
'new_post' => array(
'post_type' => 'technical_brief',
'post_status' => 'publish',
'post_title' => 'testing',
),
'return' => get_the_permalink(624) . '?bid=%post_id%',
'submit_value' => 'Upload your brief',
'form_attributes' => array(
'enctype' => 'multipart/form-data',
),
'uploader' => 'wp',
)
);
and here is the user role declaration giving permissions:
add_role( 'user', __('User'),
array(
'read' => true,
'upload_files' => true,
'edit_posts' => true,
'edit_pages' => true,
)
);
Any suggestions for allowing file uploads for the User role?
Update:
It seems that the "user" role was part of the problem. I checked the capabilities of this role, and despite me adding capabilities, it only ever shows "read", so I guess this role is reserved by WP.
Another part of the issue seems to be the file types uploaded. PDF, DOC, DOCX, and ODT all work fine, but .txt and .rtf will not upload - the form submits, but the files do not appear in the back end. As such, the issue is not 100% solved, but is working enough that it is suitable for my project. I'll leave it as not answered for now in case any further knowledge shows up later that may help.

How to make Titan Framework text field required

I am using Titan Framework with a custom plugin I wrote. There is a settings page that has a tab and on it there is a text field:
array(
'name' => 'Slug',
'id' => 'my_slug',
'type' => 'text',
'default' => 'my-slug',
'desc' => 'The slug.',
),
This text field allows the user to specify a slug for the plugin's frontend UI. The problem is that a user can clear this field out to nothing and then save settings which results in an error.
I understand that I can trap this condition in the PHP code when this setting option is used. I also understand this could be done using jQuery to enforce a value being specified, but it would seem like there should be a best practice solution.
Can anyone tell me the proper way to ensure this field has a value?
The user can definitely save it as empty and yes you can prevent that with JS. But because there is no "pre save" filter, one way to address this is to to the method you mentioned, add a placeholder attribute to the field to give the illusion of a value when empty (this is just for a better user experience).
array(
'name' => 'Slug',
'id' => 'my_slug',
'type' => 'text',
'default' => 'my-slug',
'desc' => 'The slug.',
'placeholder' => 'post',
),
Then check for empty when getting the value.
$slug = $titan->getOption( 'my_slug' );
if ( empty( $slug ) ) {
$slug = 'post';
}

How to add a custom Rules "action" in 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/).

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' );

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.

Resources