Score:0

Altering user_admin_account and user_register_form

ro flag

I'm pretty new to Drupal and I'm trying to build a Drupal 7 custom module to alter the user listings table in admin/people. I would like to display the first and last names of the users. I've already added the text fields to the user registration form in admin/config/people/account-settings/manage-fields and I've managed to store the user input in my database table. I've added the extra fields to the user listings table but can't figure out how to retrieve the values from my database table and output them.

Here's what I have so far in my .module file:

<?php

/**
 * Implements hook_form_FORMID_alter()
 * @param $form
 * @param $form_state
 * @param $form_id
 * @return $form
 */
function wdt_user_fields_form_user_admin_account_alter(&$form, $form_state, $form_id) {
  //Adding User 'First Name' field in the 'user_admin_account' form
  $name_header = array(
    'f_name' => array('data' => t('First Name'), 'field' => 'wdt.first_name'),
    'l_name' => array('data' => t('Last Name'), 'field' => 'wdt.last_name'),
  );

  //Place field
  array_splice($form['accounts']['#header'], 1, 0, $name_header);

  foreach($form['accounts']['#options'] as $key => $value) {
    $query = db_select('wdt_user_fields', 'wdt')
      ->fields('wdt', array('first_name', 'last_name'))
      ->condition('wdt.wdt_username', $key)
      ->execute()
      ->fetchAssoc();

  }

  $form['pager'] = array('#markup' => theme('pager'));
  return $form;
}

/**
 * Implements hook_user_insert()
 * @param $edit
 * @param $account
 * @param $category
 */
function wdt_user_fields_user_insert(&$edit, $account, $category) {
  db_insert('wdt_user_fields')
    ->fields(array(
      'wdt_username' => $account->name,
      'first_name' => $edit['field_first_name']['und'][0]['value'],
      'last_name' => $edit['field_last_name']['und'][0]['value'],
    ))->execute();
}

I'm pretty sure I'm doing something wrong when it comes to the $query part but I can't seem to find anything online.

In addition to this, I've used the 'name' from the 'users' table as the primary key in my custom table. I'd like my table to be updated when a user is deleted, so the first and last name in my table are deleted too. I'm not too sure how to do this. Does anyone have any ideas?

If anyone could help me with any of these questions it'd be great. Thanks in advance.

Jaypan avatar
de flag
"I'm pretty sure I'm doing something wrong when it comes to the $query" <- You haven't used the resulting data, which is why it's not being displayed. You'll need to output that data into your table to view it.
berliner avatar
bd flag
Welcome to Drupal Answers! I'm a bit confused by your question. If you have added fields to the user entity, why would you need to store the entered data for those fields? Drupal takes care of that. If I misunderstood that, consider to update your question to clarify. Did you have a look at the [realname](https://www.drupal.org/project/realname) module? I think your use case is common enough that there already is a solution that doesn't involve custom code.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.