4

I am attempting to develop my first custom component called com_image. Because of the structure and function of my project requirements, it made best sense to clone the com_contact folder and duplicate the necessary db tables/rows. I commented out lots of unnecessary structure from the component, but now I need to add something in.

How can I generate a blue/info note for the "Edit Image" (first) tab at administrator/index.php?option=com_image&view=image&layout=edit&id=1 ?

For my project, there are actually no fields in the primary panel of the first tab. The note field only needs to hold the location value which I am successfully echoing with...

<div class="row-fluid">
    <div class="span9">
        <div class="row-fluid form-horizontal-desktop float-cols" >
            <div class="span12">
                <?php echo "Path: <b>" , $this->item->location , "</b><br>"; ?>
                <?php echo "<img src=\"" , $this->item->location , "\">"; ?>
            </div>
        </div>
    </div>
    <div class="span3">
        <?php echo JLayoutHelper::render('joomla.edit.global', $this); ?>
    </div>
</div>

I don't know how to make $this->form->renderField(); work for me.

I read https://docs.joomla.org/Creating_a_custom_form_field_type/en but this only solves half of the problem.

I'd like to deliver this component with attention to mise en place for my own education -- so please only suggest best practices.

To be more specific:

Do I create a new .php file in com_image/models/fields so that I can extend JFormFieldNote? If so, how do I pass my $this->item->location value to getLabel()? And how to do I call the new custom note back from edit.php? I don't want to modify com_image/config.xml, right?

Current screenshot:

enter image description here

I think GDP's answer is not quite right for my purpose: How can I dynamically generate a form's XML from PHP?

This isn't it: Custom 'dynamic' form field creation

I reckon this is pretty close: Passing Arguments for custom field

p.s. If there is a better way to deal with the image element, I'd be happy to hear that too.

  • For the record, I knew it was risky to use a common/programmatical term like image for filenames/folders/classes/etc. I was trying to smack together a component as a proof-of-concept. Today I see that I have a conflict at /plugins/editors-xtd/image/ and now I have to go back and add prefixes to everything. – mickmackusa Sep 7 '18 at 5:25
4

Use Form::setFieldAttribute(). Typically, this is done in the model's getForm() method. However, if it's cosmetic only, it can be done in the view too.

In /administrator/com_image/models/forms/image.xml (outside of a <fieldset> is fine):

<field
    name="myNote"
    type="note"
    class="alert alert-info"
/>

In /administrator/com_image/views/image/view.html.php (just before $this->addToolbar();):

$this->form->setFieldAttribute('myNote', 'label', $this->item->location);

In /administrator/com_image/views/image/tmpl/edit.php:

echo $this->form->renderField('myNote');

To create a field without editing XML file:

$note = new \SimpleXMLElement('<field />');
$note->addAttribute('name', 'myNote');
$note->addAttribute('type', 'note');
$note->addAttribute('label', $this->item->location);
$note->addAttribute('class', 'alert alert-info');
$this->form->setField($note);
  • Yes, in display(). – Sharky Sep 3 '18 at 7:33
  • Based on your previous comment, I think you edited the wrong XML file. config.xml contains component configuration form. Forms of content items are typically found in /models/forms/ directory. – Sharky Sep 3 '18 at 7:45
  • I've decided to go with description rather than label because the text is better positioned vertically within the blue container via description. – mickmackusa Sep 3 '18 at 12:01
  • I'm not going to go the "create a field without editing the XML file" way, but can you explain where to write that $note snippet and how that should be implemented (for future researchers' benefit)? – mickmackusa Sep 3 '18 at 23:22
  • 1
    It's a way to dynamically add the field to the form. You'd put it wherever you manipulate the form. In this case, it's the view's display() method. But normally it would be in model's getForm() method. Or, if used in a plugin, in onContentPrepareForm(). – Sharky Sep 5 '18 at 19:09

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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