I'm extending Joomla 3.x with a custom component.

At this point, my model has

  1. workers: Peter (id: 1), Paul (id: 2), Mary (id: 3)
  2. roles: manager(id: 1), teacher (id: 2), advisor (id: 3)
  3. links: link roles to a worker

Now, a worker - say Mary - can have one or multiple roles. With every role comes an individual date when Mary received that role: Mary is teacher since 03/01/2016 and advisor since 02/15/2015.

My links-table:
links.id: 1, worker.id: 3, role.id: 2, date: 2016-03-01
links.id: 2, worker.id: 3, role.id: 3, date: 2015-02-15

In the Joomla backend, I have a worker and a role view. In the worker-view, I'd like to go to Mary's profile and add/delete roles.

I've never seen this.

What field type would I use for that? How is it done?

  • I would rather go for a third view for the links. With a third view you could use all Joomla functionality for your links like editing, publishing or deleting. – fruppel Mar 2 '16 at 12:41
  • @fruppel I could do this, but I have around 180 workers, 50% with up to three roles. Assigning and deleting roles in the worker form would be desirable. – michi Mar 2 '16 at 21:19
  • Users are virtual ( just records on a custom table of your db ) ? Or they are site users with actuall accounts ? – IseNgaRt Mar 9 '16 at 11:14
  • @IseNgaRt: custom table – michi Mar 9 '16 at 12:30

As I understand it, you basically want to manage the links to mary's roles directly from mary's profile form, right? I don't think you should use a special field type for this, what you want is another form matching the links-table. You should add a link form-section to the profile form, f.ex as an extra tab on the form. The easiest is to let this area be disabled before the profile is actually saved, so you have an ID to insert into the links-table. Then you add some functionality to use ajax to add links to the links table.

This means you at least need to create the following:

  • Add this file in your /templates/yourtemplate/ajax.php containing only <jdoc:include type="component" />
  • A form-view for link-editing. This is called from the profile-form, and should have a select field for role and a calendar-field for the date. The form is displayed using ajax on the profile form. Test the form and your ajax template using http://examlpe.com?option=com_yourcomponen&view=linkform&tmpl=ajax. This should only return the form, no additional html.
  • Override the form-view by copying com_yourcomponent/views/linkform/tmpl/default.php to templates/yourtemplate/html/com_yourcomponent/ajax.php and add a hidden input with name tmpl and value ajax to the form. Test with http://examlpe.com?option=com_yourcomponen&view=linkform&tmpl=ajax&layout=ajax
  • A link-controller extending JControllerForm. Possibly override the edit-function for redirect to the link form-view. This will handle input from the links-section on the profile form (create, update and delete). (You might already have this?)
  • A link model for handling the link data. (You might already have this?)
  • Ajax-functionality to load the link form-view on the profile-form. This might be a button calling $.get('index.php', 'option=com_yourcomponen&view=linkform&tmpl=ajax&layout=ajax', function(data){$('#somediv').html(data);})
  • Ajax to post the data, something like $.post($('#form-link').attr('action'), $('#form-link').serialize(), function(data){$('#otherdiv').html(data);}, linked to the submit-button of the link - form.
  • Functionality to load existing links on the profile form-view, either using ajax, or load it with the rest of the profile form.

Perhaps you have some of this functionality already.

It's hard to be more specific unless you give some more details and some code to work from.

If you have not already done this, I think you would benefit from using a component builder to create a component skeleton for you. The parts where I have written You might already have this, is code that will probably already exists if you have used a component-builder to create your application skeleton. I have been using component-creator.com, it is really good but quite expensive, and probably worth it if this is a commercial project. But I think there are also others.

  • Thank you for the overview you provide! I built a component with component-creator, which has a third view for the links. I have some below medium level of understanding of the concept behind creating MVC extensions in Joomla, AJAX is all new to me. The learning curve here seems steeper than I expected :-) – michi Mar 9 '16 at 12:40
  • If you have made your component in component-creator I should be able to guide you further, but i'll need to look at it tonight when I'm not at work :) – jonasfh Mar 9 '16 at 13:05
  • no hurry, shall I create an example component with the data above (which is obviously an example as well) to work from that? – michi Mar 9 '16 at 23:19
  • That might be the easiest. I added some more solid information to each list of things you need to do. It adds up to quite a few changes, but it is generally small changes for each step, as most of the functionality already exists. – jonasfh Mar 10 '16 at 7:56

Creating a custom field would be the cleanest way of doing it.

There's no standard field type that does what you want but you could probably create a sub-class of one them. The custom field would allow you to inject any interface JavaScript you may need as well.

A likely approach would be using the Repeatable form field as it already provides a lot of the flexibility required and obviously multiple values and using your custom field with-in that.


What I would do :

  • Create a new view ( lets call it profile )
  • At workers view ( im guessing you have a table with all workers or something like that ) have links that redirecting to Profile view

    Example: <a href="index.php?option=com_custom&view=profile&id=2"></a>

Inside that view you will have all the info of that worker (Personal Infos,Roles he is currently assigned) and you can add whatever functionality you want.

Personally i would create a multiple dropdown with all the Roles or a multiple checkbox and use ajax for adding - deleting roles.


Add 3 form fields to your workers view:

  1. manager_date
  2. teacher_date
  3. advisor_date

You can use a Joomla calendar field type to make it nice and easy for you. In your controllers save function you can take the 3 dates and add them to an array like so:

$assignments = array();
$assignments['manager_date'] = $manager_date
$assignments['teacher_date'] = $teacher_date
$assignments['advisor_date'] = $advisor_date

Where $manager_date, $teacher_date, and $advisor_date have been retrieved from the submitted form input.

JSON encode the $assignments array and save that to a text field in your workers table.

If you need to display the dates elsewhere just json_decode the data and format it with the date function of php.

Very simple to do and should accomplish exactly what you are looking for.

You should be able to add the 3 fields in administrator > components > your_component_name > models > forms > workers.xml

This is assuming that your component has a custom save function and is not just using the default Joomla table relationship. Worst case scenario is you override the save function and update function in your controller.

  • If you can't figure it out zip up the extension for me and shoot me a link to download it, I can give it a look Friday and see if I can help you with it. – Terry Carter Mar 10 '16 at 6:26
  • Thank you for that idea and the offer. It will take me some time to get there, thanks the same! I hope I can get back up to 15 rep to be able to upcode :-) – michi Mar 12 '16 at 22:29

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.