7

Default core's com_content has a built in system that, when a specific article is being edited by a user, it gets "locked", so no other user can edit it at the same time. If the user closes the window or lets the session expire without properly closing that article (clicking on "save" or "cancel", for example), that content gets "checked" and a lock icon is displayed in the backend next to it, which a superadmin will have to unlock.

My question is: how is that added to a custom component that has its own content tables? As far as I've been able to find, it looks to me that as long as you use JTable to manipulate that content, then that comes as a built-in feature for methods in that class... is that right? Or do I need to use something else like JTableContent? Do I need to add something to the tables used to store content (any specific columns)?

3

You are correct, but you don't need to go down to JTable, if you're following the Joomla way and using JControllerForm as the basis for your view controller e.g.

class MycomponentControllerMycontent extends JControllerForm {}

and using JModelAdmin as the basis for you components Model e.g.

class MycomponentModelMycontent extends JModelAdmin {}

Extending JControllerForm, gives you a built-in edit() method that sets up your model and table for you, and checks access to the record in question as well as whether the table supports checkouts (by checking that a column checked_out exists). If it does, it attempts to checkout() the record via the model.

By extending JModelAdmin you inherit it's getItem(), checkIn() and checkOut() methods. As mentioned, the model calls checkout() (which it inherits from JModelForm) and makes sure that in addition to the checked_out column your table also has one called checked_out_time.

At this point one of two things will happen if either of those columns don't exists, JModelForm fakes it and returns true (indicating job done). Otherwise, it will grab the current users, and then proceed to attempt the checkout first checking if it's already checked out to another user. At this point if finally calls the JTable implementation of checkOut() (if you haven't overridden it in with your own table definition which in J3 isn't usually required).

JTable performs exactly the same column checks, and if all is good (i.e. has a valid ID) updates the columns checked_out and checked_out_time for the record being edited.

As a side note, when you edit() an item, getItem() gets called, it loads the record via a JTable (i.e. it does a getTable() followed by load($pk). (At this point JTable also triggers your onBeforeLoad and onAfterLoad events for plugins which is nice)

The record is returned to your version of JModelAdmin, which converts it to a base stdClass object (it's used to be a JObject in times past). The if the record (now object) contains a params column/attribute it converts that to a JRegistry object for easy access.

So, you get lots of handy things for free using the Joomla way. :)

  • Woha! Thanks a lot cppl! That was a very detailed and clear reply, just the starting point I needed to go ahead knowing I'm standing on solid ground. Very much appreciated! – Isidro Baquero Nov 24 '14 at 9:07
2

You're correct in your assumptions. You need the table set up with associated logic to accomplish what you're trying to do.

As I am learning Joomla 3+, I've been using Component Creator to create an extremely simple component with a single table and just the Joomla feature I'm interested in learning. The generated code is very good, and is easy to follow without all the complexities of the usual components installed.

  • Thanks for the quick answer. I found this for Joomla 1.5, not sure if it changed too much for 3.3... docs.joomla.org/J1.5:Using_the_JTable_class#Checkout.2FCheckin And btw, I'm using CC too :) – Isidro Baquero Nov 21 '14 at 17:25
  • That looks to be about the same thing, so I'd so run with it. Its one of those things that "just work", so I'm not familiar with the intricacies. – GDP Nov 21 '14 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.