On my Drupal 7 site, I am looking for a way to only allow users with a specific role to upload PDF files. All users, however, will be able to upload other file types (e.g. jpg, mp4).
Is there a module or some code available that can accomplish that tool for me?
You dont need a module for that.
You can write a hook_form_alter and do the following
1. add help-text based on role.
if role = 1
#form['field_name']['#description'] = t('Help test 1')
elseif role = 2
#form['field_name']['#description'] = t('Help test 2')
Add custom validation to check file extension based on roles.
$form['#validate'][] = 'validate_function';
function validate_function
if role = 1
Validate file type 1
elseif role = 2
Validate file type 2
cheers!
You can use hook_form_alter to achieve this.
load your user object
global $user;
$valid_role = 'yourGivenRole';
if(in_array($valid, $user->roles)){
$form['field_name']['und'][0]['#upload_validators']['file_validate_extensions'][0] = 'pdf';
}
Related
I need to run a SQL command right after a new user account has been created (I need to obtain new User ID also). (in Drupal 7)
How can I do this?
In a custom drupal module, use hook_user_insert.
within that function the $account variable with have the new user data and user id number.
To run custom sql query, you can use drupal's db_query function.
If you need to learn about custom drupal module development, take a look at the example modules.
Build a custom module with hook_user_insert implementation.
Here's a code sample that saves the new user's id into a table called test_table.
function [YOUR_MODULE]_user_insert(&$edit, $account, $category) {
$newUserId = $account->uid;
db_insert('test_table')
->fields(array(
'user_id' => $newUserId,
))
->execute();
}
I am trying to notify users whenever a site is created in alfresco share. I created a rule for the site folder in the repository.
In Define Rule, I selected:
When: Items are created or entered in the folder
If all criteria are met: Description contains "a"
Perform Action: Send email
But in the message of the email I need to give the site name.
For example:
A new site named "Sample" is created. Click the link to join the site.
How can I get the site name and corresponding link to join the site?
You can do something like this.
var currentSite = Alfresco.constants.SITE;
var siteObject = siteService.getSite(currentSite );
If you are using javascript directly to send mail you you will have site object available .
You can also try this
siteId = page.url.templateArgs.site;
If you are using ftl you can probably pass sitename from script file to FTL file.
You have multiple options to get current site name depending on the context where you are trying to access it.
I am using Profile 2 to add fields in registration form in drupal 7.
now i want to show name fields before username and password fields, how can i do it ?
Edit: I'm sorry, I had misunderstood your question (left my previous answer for history).
Try Profile2 Registration Path. It promises to merge both your account and profile information on a custom path. Use the .htaccessfile to redirect from user/register to the new URL or install one of the various redirect modules.
Afterwards you might want to follow the approach from my previous answer to correct the order:
Use hook_form_alter to set the weights of the fields according to your needs. You can do so by inserting somthing similar to
function yourthemename_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'user_register_form') {
$form['field_firstname']['#weight'] = -20;
$form['field_lastname']['#weight'] = -19;
}
}
in the template.php of your theme.
Be aware that I'am using the build in profile fields instead of Profile2 but it should work the same way. If you're not sure how your profile fields are to be accessed download and enable develmodule, set permissions to allow guests to access developer information and insert a dpm($form)in the above function.
How can I create a custom block snippet (or download an available module) that would display a selected amount of information about a node in Drupal (6)? (i.e. author, creation date and published status)
I would later make this node available to admin user and only in certain content types, as a mean of seeing node information in-situ while browsing the web only as admin (this part I know how to achieve).
Thank you
I would use the Views & Context modules.
You can use a block display in Views to output the desired fields. Add an argument, then select the option to get the argument from the url.
Context module allows you to (among other things) set access rules based on roles.
I use both of these modules in all of my Drupal installs and find them quite helpful.
http://drupal.org/project/views
http://drupal.org/project/context
You can create a custom block (admin/build/block/add)
Make sure PHP Filter module is enabled already
For your block body select input filter as PHP Code
Add these lines in the body to load node information
<?php
if(arg(0) == 'node' && is_numeric(arg(1))){
$node = node_load(arg(1));
$user = user_load(array('uid' => $node->uid));
print "Author: " . l($user->name, 'user/' . $node->uid);
print "Creation date: " . date('m/d/y h:i:s', $node->created);
print "Publish Status: " . ($node->status) ? "Published" : "Unpublished";
}
?>
Views are totally advisable, writing custom code for something like that is a bad practice... In general the combination CCK & views is very poweerful in Drupal!
I've got a Drupal site which uses a custom field for a certain type of node (person_id) which corresponds to a particular user. I want to create a view so that when logged in, a user can see a list of nodes 'tagged' with their person_id. I've got the view working fine, with a url of my-library/username but replacing username with a different username shows a list of all nodes tagged with that user. What I want to do is stop users changing the URL and seeing other users' tagged nodes. How can I do this? Is there somewhere where I can dictate that the only valid argument for this page is the one that corresponds with the current logged in user's username?
person_id = uid?
In this case, add argument with user:uid, then in Validation options select PHP Code, read comment of this field carefully:
Enter PHP code that returns TRUE or
FALSE. No return is the same as FALSE,
so be SURE to return something if you
do not want to declare the argument
invalid. Do not use . The
argument to validate will be
"$argument" and the view will be
"$view". You may change the argument
by setting "$handler->argument".
Add this code:
global $user;
$account = user_load('name'=>arg(1));
$handler->argument = $user->uid;
return $account->uid == $user->uid;
I'm not sure how you have setup your view, which gives some different options to solve this. A way that should work would be to set the default argument be the logged users id/username and remove the argument from the url.
Alternatively you could create your own filter which requires some work with the views API, but gives more control.