1

The custom fields were created using a custom form in a module, not user entities or Profile2.

How can I expose these fields to views? My goal is to allow an admin to filter users by exposing those custom fields.

Here's the function that creates the custom profile form:

function mymodule_profile_form($form, &$form_state, $account) {
[...]
//
// Basic Info
//
$form['fs_basic'] = array(
  '#title' => 'Basic Info',
  '#type' => 'fieldset',
  '#prefix' => '<div class="row">',
);

$form['fs_basic'][SOME_SF_CONTACT_FIRSTNAME] = array(
  '#title' => 'First Name',
  '#type' => 'textfield',
  '#default_value' => $contact[SOME_SF_CONTACT_FIRSTNAME],
 );
$form['fs_basic'][SOME_SF_CONTACT_LASTNAME] = array(
  '#title' => 'Last Name',
  '#type' => 'textfield',
  '#default_value' => $contact[SOME_SF_CONTACT_LASTNAME],
);
$form['fs_basic'][SOME_SF_CONTACT_MIDDLENAME] = array(
  '#title' => 'Middle Name',
  '#type' => 'textfield',
  '#default_value' => $contact[SOME_SF_CONTACT_MIDDLENAME],
);
$form['fs_basic'][SOME_SF_CONTACT_SUFFIX] = array(
  '#title' => 'Suffix',
  '#type' => 'textfield',
  '#default_value' => $contact[SOME_SF_CONTACT_SUFFIX],
);
//
// Profile Info
//
$form['fs_profile'] = array(
  '#title' => 'Profile',
  '#type' => 'fieldset',
  '#prefix' => '<div class="row">',
);
$form['fs_profile']['name'] = array(
  '#title' => 'Username',
  '#type' => 'textfield',
  '#default_value' => $account->name,
  '#maxlength' => 20,
);
[...] 
$form['submit'] = array(
  '#value' => 'Submit',
  '#type' => 'submit',
  '#attributes' => array('class' => array('btn btn-primary align-left push-
down')),
);

$form['#validate'][] = 'user_account_form_validate';
$form['#validate'][] = 'user_validate_picture';
$form['#validate'][] = 'user_profile_form_validate';
$form['#validate'][] = 'mymoduler_profile_form_validate';
$form['#submit'][] = 'mymodule_profile_form_submit';
// Add the final user profile form submit handler.
$form['#submit'][] = 'user_profile_form_submit';

  return $form;
}

I want to be able to expose the Last Name in Views to I can allow an admin to search.

  • What is mymodule_profile_form_submit doing? – Kevin Mar 30 '17 at 18:21
  • It's used to populate the form fields into Salesforce. – Tabestan Apr 3 '17 at 16:41
0

In order to use it with Views, one of the following have to be true:

  • You are using Profile2
  • You are using core User profile
  • You are using actual fields from Field API
  • The profile storage is an entity
  • None of the above, and you are using a custom table

One of these should let you add a relationship in Views from 'User' to 'Profile2' associated to that user, if not, the core profile fields should be available.

However, if you used a custom table, you will have to do some legwork describing your data to Views in order for it to be an option.

Here are some resources that will help you in that direction:

Do know that just because they are 'fields' in a custom form does not mean that it is represented in Drupal the same way. As far as Drupal knows (and from what I see in the provided code) it is your standard form made with FormAPI. They are not really "custom fields" so much as form items abstracting the input to store data.

| improve this answer | |
  • Thanks, I was able to describe the data to views using hook_views_data. – Tabestan Apr 4 '17 at 18:01
  • Glad it helped. – Kevin Apr 4 '17 at 18:02
  • @Kevin How to expose the username, password and email field forms in panels or maybe in the manage fields? I want to use field_group module to create a vertically tabbed "Edit Profile Page". Each tab will only have relevant field form. For example: General Tab will have username, password and email fields forms. And similarly other tabs will have other fields. How to do that? – Umair May 10 '17 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.