What's the best way to get a profile field to link to its profile page?

I'm making a member's directory where the first and last name should link to that user's profile page. I don't see a "link to profile" in the views options just like "link to node" in d6.

Only way I can think of is using php custom field, and matching user profile to the user but that is long winded. Would like to avoid this way as much as possible.

CommentFileSizeAuthor
#2 profile2.zip30.08 KBroam2345

Comments

fago’s picture

Category: support » feature

Indeed, we should improve the Views integration to offer a "Link to this entity" option.

roam2345’s picture

StatusFileSize
new30.08 KB

Started looking at this and have a working 'hack' followed the commerce module patterns.

Added this to the profile2.module

/**
 * Implements hook_views_api().
 */
function profile2_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'profile2'),
  );
}


/**
 * Implements hook_field_views_data().
 */
function profile2_field_views_data($field) {
  $data = field_views_field_default_views_data($field);

	foreach ($data as $table_name => $table_data) {
    foreach ($table_data as $field_name => $field_data) {
      if (!in_array($field_name, array('table', 'entity_id', 'revision_id'))) {
        $data[$table_name][$field_name]['relationship'] = array(
		    'title' => t('Referenced users'),
		    'help' => t('Elvis went fishing'),
		    'base' => 'users',
		    'base field' => 'uid',
		    'handler' => 'views_handler_relationship',
		    'label' => t('profile2'),
		  );
      }
    }
  }
  return $data;
}

Added the profile2.views.inc file with

<?php
// $Id: profile2.views.inc,v 0.1

/**
 * Export Drupal profile2 to Views.
 */


/**
 * Implements hook_views_data()
 */
function profile2_views_data() {
  $data = array();
  
  //set the group in the view type list
  $data['profile']['table']['group']  = t('Profile2');

	//define the base table
  $data['profile']['table']['base'] = array(
    'field' => 'pid',
    'title' => t('Profile2'),
    'help' => t('Profile entities relation ship to user.'),
  );
  
  // Expose the creator uid.
  $data['profile']['uid'] = array(
    'title' => t('Creator'),
    'help' => t('Relate a Profile2 to the user who created it.'),
    'relationship' => array(
      'handler' => 'views_handler_relationship',
      'base' => 'users',
      'field' => 'uid',
      'label' => t('Profile2 creator'),
    ),
  );
  
  return $data;
}

Still rather new to the module side of things but I think its a start.

munitras’s picture

Wow. Just tried this real quick. Worked the charm -- thanks man.

Been barking up the wrong tree myself, thought it would need

 $data['profile']['table']['join']['users'] = array(...

instead of

 $data['profile']['table']['base'] = array(...

thanks for this.

roam2345’s picture

Issue tags: +code review

So I added the missing join to have the relation ship work from the reverse way round ie. from users, and moved to views_data_alter to that I don't break the existing entities that were/are coming through.

profile2.module still has this at the bottom.

/**
 * Implements hook_views_api().
 */
function profile2_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'profile2'),
  );
}


/**
 * Implements hook_field_views_data().
 */
function profile2_field_views_data($field) {
  $data = field_views_field_default_views_data($field);

	foreach ($data as $table_name => $table_data) {
    foreach ($table_data as $field_name => $field_data) {
      if (!in_array($field_name, array('table', 'entity_id', 'revision_id'))) {
        $data[$table_name][$field_name]['relationship'] = array(
		    'title' => t('Referenced users'),
		    'help' => t('Elvis went fishing'),
		    'base' => 'users',
		    'base field' => 'uid',
		    'handler' => 'views_handler_relationship',
		    'label' => t('profile2'),
		  );
      }
    }
  }
  
  return $data;
}

the new file profile2.views.inc changes to this

<?php
// basic's are working 

/**
 * Implements hook_views_data_alter(&data)
 */
function profile2_views_data_alter(&$data) {
  // The 'group' index will be used as a prefix in the UI for any of this
  // table's fields, sort criteria, etc. so it's easy to tell where they came
  // from.
  $data['profile']['table']['group']  = t('Profile');

	// Define this as a base table.
  $data['profile']['table']['base'] = array(
    'field' => 'pid',
    'title' => t('Profile'),
    'help' => t('Profile entities relation ship to user.'),
  );
  
  
  // This table references the {Users} table.
  // This creates an 'implicit' relationship to the users table, so that when 'Users'
  // is the base table, the fields are automatically available.
  $data['profile']['table']['join'] = array(
    // Index this array by the table name to which this table refers.
    // 'left_field' is the primary key in the referenced table.
    // 'field' is the foreign key in this table.
    'users' => array(
      'left_field' => 'uid',
      'field' => 'uid',
    ),
  );
  
  // Expose the creator uid.
  $data['profile']['uid'] = array(
    'title' => t('Creator'),
    'help' => t('Relate a Profile to the user who created it.'),
    'relationship' => array(
      'handler' => 'views_handler_relationship',
      'base' => 'users',
      'field' => 'uid',
      'label' => t('Profile creator'),
    ),
  );
}
goldlilys’s picture

Added this code, but still unsure how it's supposed to work. Is this supposed to show up when you have a field to display and there is a checkbox that says link to profile? Or other steps are needed for this to work?

roam2345’s picture

It adds a relation ship to a view ;)

fago’s picture

for the relationship, please test the patch from #1066398: Reverse entity relationships for Views integration

Herbt’s picture

After adding the code, I see the ability to list user id (unless that was already there and I hadn't noticed it). How would I go about using that to generate a linked list of user names?

My ideal would be for views to generate a linked list of users to their profiles based on tags, but I'll take anything at the moment. I actually get the results I need by building a custom content type. I like the way profile2 integrates into the user setup though.

Thanks

roam2345’s picture

Hey Herbt,

You should apply the entity api patch not the one I posed above.
- Then I assume you view is on a profile, then you would add the relation ship of user.
- After that you can pull what ever field off user you want.. ie (link to user page, as you wanted).

Wallace123455’s picture

Hello, I applied the code written by jucallme and it seems to be working. I can create a relationship "(Profile creator) User: Profile".

However, in my fields I still cannot find any field I can select which will have a link to the users profile page.

My ultimate goal is to create a view for the manager in an office to be able to link through the view to each employees profile page.

Am I doing something wrong? I feel like I am almost there, just don't know how to complete the final stretch.

Thanks

Wallace123455’s picture

I think I figured it out. In my configuration of Profile2, I had "Provide a seperate page for editing" enabled under the profile type configuration page. Once I unchecked that, my Profile information is stored on the main user page. Therefore, it works.

Does anyone know how I can use this solution with the "Provide a seperate page for editing" enabled?

Thanks

roam2345’s picture

Please read #9

bjlewis2’s picture

You could also do it rather simply, using views. Follow these instructions for creating a contact link: http://drupal.org/node/1311706, only leave off the "/contact" and it should take you to the profile page.

fago’s picture

Status: Active » Fixed

I think there is now a field "Rendered profile" in views once you have the latest entity api module installed, which has the display option to show just the entity label as link to the entity.

Status: Fixed » Closed (fixed)
Issue tags: -code review

Automatically closed -- issue fixed for 2 weeks with no activity.