This is the documentation for concrete5 version 5.6 and earlier. View Current Documentation

Originally posted by ChadStrat at stratisdesign.com

 


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 and UserInfo models, and then walk through how to pull certain attributes for your users and display them.

The User and UserInfo classes are already loaded by the core by the time your blocks and pages need to access them - so you don't even need to worry about loading them using Loader::model().

Step One: Get the User object for the currently logged in user

Step Two: Grab the UserInfo object for that user's ID

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. This is the UserInfo object.  So we will statically call this class, and snag the user's info by using the current $user object, and referencing its uID (user ID). 

Step Three: Get the information you need

Now that we have our user info object, we can access a host of information.  I will be covering some commonly used methods, and then also cover how to access custom user attributes.

UserName

Email Address

Other Information

A list of important methods found in the User and UserInfo objects can be found in the user documentation.

Displaying Custom User Attributes

Before moving on, head to your concrete5 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:

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 download the Hello World block found here:

Creating a New Block Type


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.

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.

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!

Loading Conversation