In this tutorial I am going to walk through some quick and easy ways to access user info with your pages, blocks, or packages.
We will take the simple approach of calling the user model and user object, and then walk through how to pull certain attributes for your users and display them.
The Building Blocks
Call the user model
<?php
Loader::model(‘user’);
Loader::model(‘userinfo’);
?>
This loads in the model for us call the user object, and then also loads the model for our user information.
Create a new user object
<?php
$user = new user();
?>
Now lets specify the user info function within this object to grab all the user info.
Because the actual user object is more for identifying and adding users, the core team extended that class with a class for pulling user related info specifically. So we will statically call this class, and snag the users info by using the current $user object, and referencing it’s uID (user ID).
<?php
$u = UserInfo::getByID($user->uID);
?>
Commonly used $user Vars
Now that we have our user info object, we can access a host of information. I will be covering some commonly used variables, and then also cover how to access custom user attributes using the same method.
$uName - User Name
<?php
$uName = $u->uName;
echo “Hello $uName, great to see you again!”;
?>
So you can see here, we instance the var $u, which is our UserInfo object. All standard user vars will be “contained” within this object. I will cover how to access custom vars here shortly.
$uEmail - User Email
<?php
$uEmail = $u->uEmail;
echo “Send $uName an email <a href=\”mailto:$uEmail\”>HERE</a>”;
?>
You get the idea!
What all is available?
Now, you can check out the Concrete5 API HERE, or you can also just add to your code a var dump and exit which will print out all the available vars within the UserInfo object.
<?php
var_dump($u);
exit;
?>
Accessing Custom User Variables
Before moving on, head to your C5 Dashboard, Users & Groups, And create a new text attribute with a handle name of “user_fullname”, and a name of “User Name”. Mark it as required, searchable, and editable.
Now update your own user information with your full name.
So we have a Full Name attribute, but how do we access and display that?
The same way we accessed standard user vars, but with a twist. The UserInfo class has a nifty method/function for accessing custom attributes called a “magic function”. This neat little guy allows you to create a function on the fly named in CamelCase(no underscores, each name sake capitalized) appended to the function “getUser”.
So, taking our user_fullname custom user attribute as an example, we would call it like so:
<?php
$fullname = $u->getUserUserFullname();
echo “Hello $fullname ($uName), great to see you again!”;
?>
Pretty easy!
Putting it all together with a block
Lets put this all to practice by creating a nice “Hello User” block!
First, you will want to go to the C5 site, and download the example block.
http://www.concrete5.org/documentation/how-tos/understanding-and-building-blocks-in-concrete5/
Lets do some renaming of the files, folders, and install script before we install this.
First, rename the folder to “hello_user”.
Second, using CamelCase, we want to appropriate the class in the controller.php file. We also want to adjust the btDescription, btName, and most importantly, the btTable.
<?php
class HelloUserBlockController extends BlockController {
var $pobj;
protected $btDescription = "A simple greeting block.";
protected $btName = "Hello User";
protected $btTable = 'btHelloUser’;
protected $btInterfaceWidth = "300";
protected $btInterfaceHeight = "150";
}
?>
Third, we will modify the db.xml file. We will get rid of the content field, and just have the block id for versioning, and then also rename the table to match the btTable var defined in the step above.
<?xml version="1.0"?>
<schema version="0.3">
<table name="btHelloUser">
<field name="bID" type="I">
<key />
<unsigned />
</field>
</table>
</schema>
Last, we will remove the content entry from the add.php and edit.php files. You can simply delete all lines of code from these two files. If for some reason, you wanted to add some fields to the display, or vars that can be set “per block”, these two files are where you would do that. So for example, if I wanted the user to be shown as either uName OR fullname, then I might have a simple checkbox named “name_display” in these add/edit block forms. And then simply add that field type “name_display” to the table schema that we defined above.
Now, lets add all the code we did above for our variables into the view.php file, and we are all done!
<?php
//load in our models so that we can pull some class'
Loader::model('user');
Loader::model('userinfo');
//create a new user object
$user = new user();
//if a user is logged in, show one message, if not, show a generic guest message
if ($user->isLoggedIn()){
//instance the UserInfo Class
$u = UserInfo::getByID($user->uID);
//pull the userName from the UserInfo class
$uName = $u->uName;
//user the special magic function to grab our custom var, user_fullname
$fullname = $u->getUserUserFullname();
//========================================//
//here is where we do some content stuff
//========================================//
echo "Hello $fullname ($uName), great to see you again!";
}else{
echo "Hello Guest! Welcome to our site!";
}
//========================================//
//This is the end of our content stuff
//========================================//
?>
Chad Cantrell
Designer, Programmer, Author, Digital Strategist.
Please add a comment
Chad,
Thanks for this great tutorial on how to actually construct a real live block. I've read a lot of things on this but I think you’ve done the best job yet showing how and why, and the code doesn’t look too daunting. I'm going to give this a try.
Steve
Thanks for the tutorial man.
Mike,
online casino
Chad,
I've gone through this and created the block but Im getting a syntax error when I try to implement the block. I've copied and pasted the view.php to save time but I've looked at the code block steps in the top of the tutorial and all the code looks good. Could you see yoiur way to post a zip of the completed code block so I can see the error of my way?
Also, the database entry contains the bID value of "54". Is this really needed? Would this block run without a db.xml file? Im wiling to experiment.
Thanks,
Steve
@steve,
great idea about adding a downloadable file! I will see about doing that in the near future.
The db.xml is really just a matter of habit, getting yourself acclimated to the database structure, and also versioning. Not that versioning matters much with no database values, but as mentioned in the tutorial, you could add vars to be displayed easily. For example, you could add in the add block dialog a text field to be displayed for guests, and a text field to be displayed for registered users. This would be really easy as set up now. just add two "C" feilds to the schema, then add the two inputs named the same in your add/edit forms. Done. Then you can print those vars using if/else statements in the display of the block for example if that is your guest field name in the form and table.
great tutorial, love C5 but building blocks has always been a bit daunting so far, but I think this might just help to make it all click in to place.
Cheers
Allan
Chad, great stuff!
Could this be used to show previously viewed objects (pages or products) for a specific user or even for a specific ip address?
@Allen. Thanks for reading.
Posted via ProBlog Mobile App
I'm so glad I found my soulotin online.
Holy concsie data batman. Lol!
God, I feel like I sohlud be takin notes! Great work



