Drupal 7 Rules: how to send within an e-mail the url of an uploaded attachment (after node change)? - drupal

I have a Drupal 7 site with a custom node type with a custom attachment field. After a (kind of this custom) node has been published, I got an e-mail with the help of Rules module. I would like to set up an another rule, which sends me an another e-mail, when somebody edits this node and uploads an attachment. Inside the mail, I want to see the full url to the uploaded file.
After a several test, I'm not able to insert the full path to the uploaded file. The [node:field-attachment:file] doesn't work at all. I'm able to insert (manually) the url where this file resides, but after I can not insert the file name (so even the filename would be enough for me).
So with the [node:field-attachment:file] I got an error: *Fatal error: Call to a member function value() on a non-object in /home/xxx/sites/all/modules/entity/entity_token.tokens.inc on line 297.*
Which is exactly this:
/**
* Gets the token replacement by correctly obeying the options.
*/
function _entity_token_get_token($wrapper, $options) {
if ($wrapper->value() === NULL) {
// Do not provide a replacement if there is no value.
return NULL;
}
With the [node:field-attachment], I'm getting the e-mail, but it contains this: You can download the file from here: Property 0
Which token should I use to get the url (or the filename) of the actually uploaded file when I edit the node?

2 solutions
1 - You can write a simple module that hooks the node submit and handle the URL there and then mail it.
2 - You can create an action in rules module to execute a custom PHP code. There you will get all details of node in $node. You can also send an email using drupal mail API

Related

SilverStripe 4.1 Email->addAttachment()?

I have a contact form that accepts a file input, I'd like to attach the file to the email that gets sent from the form.
Looking at the API reference isn't really helping, it states that the function expects a filepath with no clarification on anything beyond that.
The submit action will save a record of the into the database and this works correctly, something like:
$submission = MyDataObject::create();
$form->saveInto($submission);
$submission->write();
an Email object then gets created and sent. Both of these are functioning and working as expected.
Trying to attach the File I've tried:
$email->addAttachemnt($submission->MyFile()->Link());
which is the closest I can get to getting a filepath for the document. Dumping and pasting the resulting filepath being output by that call will download the form but that line throws an error and can't seem to locate the file.
I suspect that I'm misunderstanding what's supposed to be given to the function, clarification would be very much appreciated.
P.S. I don't currently have access to the code, I'm looking for some clarification on the function itself not an exact answer :).
In SilverStripe 4 the assets are abstracted away, so you can't guarantee that the file exists on your webserver. It generally will, but it could equally exist on a CDN somewhere for example.
When you handle files in SilverStripe 4 you should always use the contents of the file and whatever other metadata you have available, rather than relying on filesystem calls to load it.
This is how the silverstripe/userforms module attaches files to emails:
/** #var SilverStripe\Control\Email\Email $email */
$email->addAttachmentFromData(
$file->getString(), // stream of file contents
$file->getFilename(), // original filename
$file->getMimeType() // mime type
);
I would try $email->addAttachment($submission->MyFile()->Filename); If it doesn't work, you may need to prepend $_SERVER['DOCUMENT_ROOT'] to the filename.
$email->addAttachment($_SERVER['DOCUMENT_ROOT'] . $submission->MyFile()->Filename);

Getting the site name in alfresco email notification

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.

Drupal user permissions & odd content types

I have a permissions problem in Drupal. I want users to be able to create a certain node type, but there are two different paths I need to give them permissions for to let them do this. The type is content created by a module called isbn2node, and there are two ways to make content through it, each with different paths:
?=node/add/isbn2node-book
?=node/add/isbn2node_book/isbn2node
One has an underscore and the other one has a hyphen. The first path leads to a form that lets users enter information on a book manually; the second path lets them enter an ISBN, searches for it, and populates the form for them based on the results.
I've changed permissions in the People menu so they can add isbn2node-book content manually using the first path, but there isn't an option to let them use the second method. Aliasing the url so it didn't have node/add in the path didn't work either.
Creating a duplicate content type seems like an ugly solution to this; is there a more elegant way to let users access that second path?
A little code in a custom module using hook_node_access should do it.
$node is either a node object or the machine name of the content type on which to perform the access check (if the node is being created then the $node object is not available so it will be a string instead).
So this should do it:
function MY_MODULE_node_access($node, $op, $account) {
if ($op == 'create') {
$type = $node;
if($type == 'book' && $account->uid) return NODE_ACCESS_ALLOW;
}
}
I figured this out, and the issues I was having were specific to this content type. The ISBN2Node module requires users to have the Administer Nodes permission to use its lookup and bulk import features.
There is some extra code for the module's hook_permission and hook_menu sections submitted as a fix in the module's issues thread.

Modify the title given in hook_menu() from another module

How can I change the title given to the /user/[uid] page from Your account to Welcome [user name] for logged-in user, where [user name] is the username for the currently logged-in user?
Use hook_menu_alter.
Alter the data being saved to the {menu_router} table after hook_menu is invoked.
Code sample:
function MYMODULE_menu_alter()
{
global $user;
if($user->uid != 0)
$items['user']['title'] = 'Welcome ' . $user->name;
}
You should be able to accomplish this with the Menu token module:
Menu Token module provides tokens, that could be used in title or in path of menu items (links).
(It requires the popular Token module.)
Note that with Drupal 7.23, the user.module includes a 'title callback' to determine if the user is logged in or not, and respond with a corresponding title.
Code that worked for me (through theme template, instead of a custom module):
function YOURTHEME_menu_alter(&$items) {
$items['user']['title callback'] = 'YOURTHEME_user_menu_title';
}
function YOURTHEME_user_menu_title() {
global $user;
return user_is_logged_in() ? t($user->name) : t('User account');
}
The String Overrides module should make this easy.
In Drupal 7, hook_menu() and hook_menu_alter() are just invoked when the data about routes implemented from modules needs to be refreshed, for example when a module is enabled, disabled, installed, or uninstalled. An implementation of hook_menu_alter() that uses the name of the currently logged-in user in the title would show the same username for different users.
Differently, the title callback associated with a route is called every time the page associated with that route is rendered.
The correct code would be similar to the following one.
function mymodule_menu_alter(&$items) {
$items['user']['title callback'] = 'mymodule_user_profile_title';
}
function mymodule_user_profile_title() {
global $user;
return user_is_logged_in() ? t('Welcome, #name', array('#name' => format_username($user))) : t('User account');
}
Notice that the first argument of t() needs to be a literal string, not a dynamic value as in t($user->name) because the database table containing the string translations would not contain the translation for every username used in a site.
It is also wrong because the shown username is invariant respect the language used on a site: For example, in an Italian site, a username like Albert isn't translated to Alberto, nor Vincent is translated to Vincenzo.
When showing a username in the UI, it is always preferable to use format_username(), which allows to third-party module to change what shown as username. (For example, a module could show the content of a user field, instead of showing the login username.)
If you aren't willing to write custom code, you could use the String Overrides module. If you don't want to use any module just for changing the title of the user profile page, you could add the following code in the settings.php file used for the site.
$conf['locale_custom_strings_en'][''] = array(
'My account' => 'Welcome to my site'
);
Notice that, either using the String Overrides module or adding the $conf['locale_custom_strings_en'][''] array in the settings.php file, you cannot:
Provide a string that changes basing on the logged-in user (which is what the question is asking for)
Provide a string that is used only on specific pages
The latter case could be a pro or a con. If the string that needs to be changed is generic enough, it would be replaced even when it should not.

Re-processing attached images in drupal 7

I'm trying to import nodes from my forum to drupal 7. Not in bulk, but one by one so that news posts can be created and referenced back to the forum. The kicker is that I'm wanting to bring image attachments across as well...
So far, using the code example here http://drupal.org/node/889058#comment-3709802 things mostly work: Nodes are created, but the images don't go through any validation or processing.
I'd like the attached images to be validated against the rules defined in the content type. in particular the style associated with my image field which resizes them to 600x600.
So, instead of simply creating the nodes programatically with my own form, i decided to modify a "new" node using hook_node_prepare and using the existing form to create new content (based on passed in url args). This works really well and a create form is presented pre-filled with all my data. including the image! very cute.
I expected that i could then hit preview or save and all the validation and resizing would happen to my image, but instead i get the error:
"The file used in the Image field may not be referenced."
The reason for this is that my file doesn't have an entry in the file_usage table.. *le sigh*
so, how do i get to all the nice validation and processing which happens when i manually choose a file to upload? like resizing, an entry in the file_usage table.
The ajax upload function does it, but i can't find the code which is called to do this anywhere in the api.
What file upload / validation functions does Drupal call which i'm not doing?
Anybody have any experience with the file/image api for Drupal 7 who can help me out?
For getting the usage entry (in essence, checking out a file to a specific module so that it doesn't get deleted while its in use) look up the Drupal function 'file_usage_add()'
For validating incoming images, I got this example from user.module (if you're comfortable with PHP, you can always look at the core to see how something is done the 'Drupal way'):
function user_validate_picture(&$form, &$form_state) {
// If required, validate the uploaded picture.
$validators = array(
'file_validate_is_image' => array(),
'file_validate_image_resolution' => array(variable_get('user_picture_dimensions', '85x85')),
'file_validate_size' => array(variable_get('user_picture_file_size', '30') * 1024),
);
// Save the file as a temporary file.
$file = file_save_upload('picture_upload', $validators);
if ($file === FALSE) {
form_set_error('picture_upload', t("Failed to upload the picture image; the %directory directory doesn't exist or is not writable.", array('%directory' => variable_get('user_picture_path', 'pictures'))));
}
elseif ($file !== NULL) {
$form_state['values']['picture_upload'] = $file;
}
}
That function is added to the $form['#validate'] array like so:
$form['#validate'][] = 'user_validate_picture'

Resources