home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (user_from_userref.inc)

This example Drupal source code file (user_from_userref.inc) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

array, conf, context, form, function, if, isset, options, php, reference, return, title, uid, user

The user_from_userref.inc Drupal example source code

<?php
// $Id: user_from_userref.inc,v 1.1.2.3 2009/07/21 20:06:12 markuspetrux Exp $

/**
 * @file
 * Implements the user reference relationship for Panels.
 */

/**
 * Implementation of hook_ctools_relationships().
 */
function userreference_user_from_userref_ctools_relationships() {
  return array(
    'title' => t('User from reference'),
    'keyword' => 'userreference',
    'description' => t('Adds a user from a user reference in a node context; if multiple users are referenced, this will get the first referenced user only.'),
    'required context' => new ctools_context_required(t('Node'), 'node'),
    'context' => 'userreference_user_from_userref_context',
    'settings form' => 'userreference_user_from_userref_settings_form',
    'settings form validate' => 'userreference_user_from_userref_settings_form_validate',
  );
}

/**
 * Return a new ctools context based on an existing context.
 */
function userreference_user_from_userref_context($context, $conf) {
  // If unset it wants a generic, unfilled context, which is just NULL.
  if (empty($context->data)) {
    return ctools_context_create_empty('user', NULL);
  }

  // Prevent whitescreens on stale data.
  if (empty($conf)) {
    return ctools_context_create_empty('user', NULL);
  }

  if (isset($context->data->{$conf['field_name']}[0]['uid']) && ($uid = $context->data->{$conf['field_name']}[0]['uid'])) {
    if ($account = user_load(array('uid' => $uid))) {
      return ctools_context_create('user', $account);
    }
  }
}

/**
 * Settings form for the ctools relationship.
 */
function userreference_user_from_userref_settings_form($conf) {
  $options = array();
  foreach (content_fields() as $field) {
    if ($field['type'] == 'userreference') {
      $options[$field['field_name']] = t($field['widget']['label']);
    }
  }
  $form['field_name'] = array(
    '#title' => t('User reference field'),
    '#type' => 'select',
    '#options' => $options,
    '#default_value' => isset($conf['field_name']) ? $conf['field_name'] : '',
    '#prefix' => '<div class="clear-block">',
    '#suffix' => '</div>',
  );

  return $form;
}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal user_from_userref.inc source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.