Drupal7 - How to change page title on user profile page with custom fields that were added? - drupal

I am trying to replace the title of the user profile page, which is the username, with other fields that were added later. Trying to show the full name as the title of the page.
I edited page.tpl.php $title with following code but it doesn't work.
if(arg(0)=="user")
{
$title=$field_user_last_name;
}

Install the RealName module and go to /admin/config/people/realname to change the pattern used for the user's name.
I'm using Profile2 to add the first and last name field and the following pattern:
[user:profile-person:field_first_name] [user:profile-person:field_last_name]
But you can probably add the fields at /admin/config/people/accounts/fields without using Profile2 aswell.

A cleaner way to do this:
function mymodule_page_alter() {
global $user;
if(drupal_get_title() === $user->name) {
drupal_set_title('New value');
}
}

You can accomplish this functionality nicely using the Entity API module's metadata wrapper and hook_user_view(). Add a dependency to Entity API in your module's .info file and then the code could look something like the following (assuming you want to set the title to the user's full name and you have fields called field_first_name and field_last_name):
/**
* Implements hook_user_view().
*/
function MYMODULE_user_view($account, $view_mode, $langcode) {
// Set the page title of the user profile page to the user's full name.
$wrapper = entity_metadata_wrapper('user', $account);
$first_name = $wrapper->field_first_name->value();
$last_name = $wrapper->field_last_name->value();
if ($first_name && $last_name) {
drupal_set_title($first_name . ' ' . $last_name);
}
}

I got solution!!!
I added following code on preprocess_page function on my template.php
if(isset($variables['page']['content']['system_main']['field_name'])){
$title=$variables['page']['content']['system_main']['field_name']['#object']->field_name['und'][0]['value'];
drupal_set_title($title);
}

Related

Change Review Display Name on Woo-commerce Store

I want to be able to change the name that shows up on the reviews on a woocommerce platform. I have found some information but I am new to WordPress and no one seems to have the answer. Here is a link that I was able to find something but no installation specifics.
I need to know where and how to change this please. Thanks.
Here is the link that I found.
https://silicondales.com/tutorials/woocommerce-tutorials/woocommerce-change-review-author-display-name-username/
I have added a plugin called Username Changer and it let me change the username of the account but it won't update the review username.
Here are some images as of now (3/6/2017)
Snapshot of how user is configured with edited username:
Ok I have figured it out. You will need to post the code into the functions.php file if you do not have a child theme. It is recommended that you set one up but I haven't yet and I just wanted to get this fixed first. Here is a snapshot of what you need to too.
Add the below block of text to the bottom or your custom-functions.php or functions.php page within Appearance > Editor
add_filter('get_comment_author', 'my_comment_author', 10, 1);
function my_comment_author( $author = '' ) {
// Get the comment ID from WP_Query
$comment = get_comment( $comment_ID );
if (!empty($comment->comment_author) ) {
if($comment->user_id > 0){
$user=get_userdata($comment->user_id);
$author=$user->first_name.' '.substr($user->last_name,0,1).'.'; // this is the actual line you want to change
} else {
$author = __('Anonymous');
}
} else {
$author = $comment->comment_author;
}
return $author;
}
Make sure to update the user info.
Missing Sections from the Original Question

How can I redirect a Drupal user after they create new content

I want to redirect my users after they create a piece of new content, instead of directing them to the 'view' page for the content.
I have seen mention of using hook_alter or something, but I'm really new to drupal and not even sure what that means?
Thanks!
As you mention that you are new to Drupal, I'd suggest to take a look at the Rules module. You can add a trigger on for content has been saved/updated and add an action, to redirect the user to a specific page.
You can however do the same in a light weight custom module using a form_alter hook.
First, find the form ID of the form. For node forms, it's the [node-type]_node_form.
Then, you can add a new submit function to be executed when the form is submitted. In this submit handler, set the redirect path.
See this guide on a basic how-to on creating a module.
Your module code would be something like belows:
<?php
function mymodule_mytype_node_form_alter(&$form, &$form_state) {
$form['#submit'][] = 'mymodule_node_submit_do_redirect';
}
function mymodule_node_submit_do_redirect($form, &$form_state) {
$form_state['redirect'] = 'my_custom_destination';
}
A much much simpler approach is to set the destination in the node form's URL.
For example, if you opened http://example.com/node/add/mytype?destination=my_custom_destination, you will be redirected to that URL instead of the node view page.
This works for me using Drupal 7, after creating a new module!
Put into the .module file the following PHP code:
<?php
function custom_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == '[node-type]_node_form') {
$form['actions']['submit']['#submit'][]= 'my_custom_submit_handler';
}
}
function my_custom_submit_handler($form, &$form_state) {
$form_state['redirect'] = 'http://www.expamle.eu/?q=node/2';
}
You just need to change [node-type]_node_form with your node type name (e.g.: example_node_form) and the http://www.expamle.eu/?q=node/2 with the correct URL.

how do I call my theme preprocess function for a specific field?

i'm on Drupal 7 and i have aspecific tpl.php file for a content field_image: "field--field_image.tpl.php". I need to create a preprocess function for this field and for my theme.
Supposing my theme name is "My Theme"
It should look like
function my_theme_preprocess_field(&$variables, $hook) {
$variables['classes_array'][] = 'aClassName';
}
but it doesn't work. I am wrong. But where?
Thanks
You can use template_preprocess_field() (like you do in your code above) but just test the particular field is the right one for you:
function my_theme_preprocess_field(&$variables, $hook) {
$element = $variables['element'];
if (isset($element['#field_name'])) {
if ($element['#field_name'] == 'field_image') {
$variables['classes_array'][] = 'aClassName';
}
}
}
Once you've implemented the hook don't forget to clear your caches, hook implementations are cached in Drupal 7 so won't be picked up until the cache is cleared.
You could declare a mytheme_preprocess_field(&$variables, $hook) in your theme's template.php where you can check your field and do operations on its label or markup, add classes, anything. So you wouldn't need field specific tpls. - eg.
function mytheme_preprocess_field(&$variables, $hook) {
if ($variables['element']['#field_name'] == 'field_machine_name') {
$variables['items'][0]['#markup'] = 'add custom markup';
}
}
Hope this helps someone.
In drupal 7 you can rewrite output of the field in template_preprocess_node() by altering "#markup" value of the field.
Also you can use regexp to change whatever you want in page content :)

How to change the title of comment form in drupal

At my website there is "Login or register to post comments.."
I want to change the message to:
"Login or register to post comments(comments will be moderated).."
Because plenty of spammers are just creating logins to post spam comments.
Although all comments are moderated now. Putting this message will give clear info that spamming may not be possible and spammers will not create logins.
Assuming you are using Drupal 6, the code that creates the comment form is in the file comments.module in the Drupal module directory. Fortunately, the function allows for theming.
What you can do is copy and paste the function theme_comment_post_forbidden($node) and place it in the template.php file in your theme directory. You will also need to rename the function, replacing 'theme' with your theme name.
For example, say your theme name is 'utilitiesindia'. Then you will rename your function to utilitiesindia_comment_post_forbidden.
So, in your template.php file in a theme named utilitiesindia, use this function:
/**
* Theme a "you can't post comments" notice.
*
* #param $node
* The comment node.
* #ingroup themeable
*/
function utiltiesindia_comment_post_forbidden($node) {
global $user;
static $authenticated_post_comments;
if (!$user->uid) {
if (!isset($authenticated_post_comments)) {
// We only output any link if we are certain, that users get permission
// to post comments by logging in. We also locally cache this information.
$authenticated_post_comments = array_key_exists(DRUPAL_AUTHENTICATED_RID, user_roles(TRUE, 'post comments') + user_roles(TRUE, 'post comments without approval'));
}
if ($authenticated_post_comments) {
// We cannot use drupal_get_destination() because these links
// sometimes appear on /node and taxonomy listing pages.
if (variable_get('comment_form_location_'. $node->type, COMMENT_FORM_SEPARATE_PAGE) == COMMENT_FORM_SEPARATE_PAGE) {
$destination = 'destination='. rawurlencode("comment/reply/$node->nid#comment-form");
}
else {
$destination = 'destination='. rawurlencode("node/$node->nid#comment-form");
}
if (variable_get('user_register', 1)) {
// Users can register themselves.
return t('Login or register to post comments(comments will be moderated)', array('#login' => url('user/login', array('query' => $destination)), '#register' => url('user/register', array('query' => $destination)))
);
}
else {
// Only admins can add new users, no public registration.
return t('Login to post comments', array('#login' => url('user/login', array('query' => $destination))));
}
}
}
}
how to change the comment link text " Login or register to post comment" to be shorter ,eg " comment"
Also you can use String overrides module.
In Drupal 6:
Another option is to create a small custom module. This uses hook_link_alter(). This is a small example module to change the title of the "Login to post new comment" link: (Replace every instance of MYMODULE_NAME with the name you choose for the module)
STEP 1: Create a file called MYMODULE_NAME.info and add:
; $Id$
name = "MYMODULE_NAME"
description = "Change the appearance of links that appear on nodes"
core = 6.x
STEP 2: Create file called MYMODULE_NAME.module and add:
<?php
// $Id$;
/**
* Implementation of hook_link_alter
*/
function MYMODULE_NAME_link_alter(&$links, $node){
if (!empty($links['comment_forbidden'])) {
// Set "Login to post new comment" link text
$links['comment_forbidden']['title'] = 'NEW TEXT';
// Add this to allow HTML in the link text
$links['comment_forbidden']['html'] = TRUE;
}
}
STEP 3: Put these files in a folder called MYMODULE_NAME, place the folder in sites/all/modules, and enable the module
If you actually want to stop spammers from creating accounts, you should use something like the CAPTCHA module, since they typically use bots that will ignore instructions in any case.
http://drupal.org/project/captcha

changing drupal 6 form and submit for exsiting content type

hey i got a contnet type that i made and i want to twweak it some , when i do form alter its all good but once i do submit its ignore my changes that i added a field for example, how can i do that changes and tell him to insert also that field? do i need to put all the fields again in hook_submit?
$function YOURMODULE_form_alter(&$form, &$form_state) {
//...
$form['#submit'][] = 'YOURMODULE_submitfunction';
//...
}
function YOURMODULE_submitfunction($form, &$form_state) {
// Save your own changes here to DB or something other
}

Resources