t("Username"), // keyword to use for %substitution 'keyword' => 'username', 'description' => t('Creates a user context from a username.'), 'context' => 'ctools_argument_username_context', 'placeholder form' => array( '#type' => 'textfield', '#description' => t('Enter the username of a user for this argument'), ), ); return $args; } /** * Discover if this argument gives us the user we crave. */ function ctools_argument_username_context($arg = NULL, $conf = NULL, $empty = FALSE) { // If unset it wants a generic, unfilled context. if ($empty) { return ctools_context_create_empty('user'); } // We can accept either a node object or a pure nid. if (is_object($arg)) { return ctools_context_create('user', $arg); } if (is_numeric($arg)) { // If user id is entered, use it to retrieve the account. $account = user_load(array('uid' => $arg)); if (!$account) { return NULL; } return ctools_context_create('user', $account); } else{ // Otherwise, turn the username into a user id, and then retrieve the account. $sql = "SELECT uid FROM {users} WHERE name = '%s'"; $uid = db_result(db_query($sql, $arg)); $account = user_load(array('uid' => $uid)); if (!$account) { return NULL; } return ctools_context_create('user', $account); } }