0

for curiosity: I'm using this method to add fields to the standard articles: https://docs.joomla.org/Adding_custom_fields_to_core_components_using_a_plugin/de

I have to use this, since the fields-component does not have subforms, but i prefer this anyway, since i have more control if i write it as plugin.. This has worked great in the last few years, but i have one little issue: When i want to use a custom form field type inside the subform (with the multiple attribute, so that i can create multiple items), i do not know how to set/get the saved value, for instance if i have a select, i have to get the saved value somehow when reopening the article. If it was a single field thats no problem, just get the attributes and get the specific value. But since it is a list, how do i get the current value of that field?

Thanks in advance and best regards

Edit:

The code inside the main form xml:

    <fieldset name="newset" label="New Set">
        <field
            name="person"
            type="subform"
            formsource="plugins/content/cfields/forms/person.xml"
            multiple="true"
            min="1"
            max="99" />
    </fieldset>

The person xml:

<?xml version="1.0" encoding="UTF-8"?>
<form>
    <field
        type="building"
        name="building"
        label="Building person works in" />
    <field
        type="text"
        name="name"
        label="Name" />
</form>

Field code:

<?php
defined('_JEXEC') or die();

jimport('joomla.form.formfield');

class JFormFieldBuilding extends JFormField
{
    protected $type = 'Building';

    private function getBuildings()
    {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select('*')->from($db->quoteName('#__buildings'));
        $db->setQuery($query);
        $result = $db->loadObjectList();

        return $result;
    }

    public function getInput()
    {
        $rows = $this->getBuildings();
        $return = '<select id="'.htmlspecialchars($this->id, ENT_QUOTES).'" name="'.htmlspecialchars($this->name, ENT_QUOTES).'">';

        foreach($rows as $row)
        {
            $return .= '<option value="'.htmlspecialchars($row->alias, ENT_QUOTES).'">';
            $return .= htmlspecialchars($row->title, ENT_QUOTES);
            $return .= '</option>';
        }

        $return .= '</select>';
        return $return;
    }
}

Since this field will be repeated and saved in the attributes column of the current article, how do i find out the current value?

2
  • 1
    Please add the relevant portions of your code so the that this question has context. Try to isolate the parts of your code that are working from the parts that are not. This will help us to help you.
    – mickmackusa
    Jul 7, 2018 at 22:42
  • you have been online recently. Please progress this page to a system-recognised resolution.
    – mickmackusa
    Aug 21, 2020 at 4:27

1 Answer 1

1

If you're asking how to mark the value as selected in Building field, compare the list values with current value ($this->value) and add selected attribute when values match:

    foreach($rows as $row)
    {
        $return .= '<option value="'.htmlspecialchars($row->alias, ENT_QUOTES).'"';
        $return .= $this->value == $row->alias ? ' selected>' : '>';
        $return .= htmlspecialchars($row->title, ENT_QUOTES);
        $return .= '</option>';
    }

Or you could extend JFormFieldList instead. It already handles this.

defined('_JEXEC') or die();

jimport('joomla.form.formfieldlist');

class JFormFieldBuilding extends JFormFieldList
{
    protected $type = 'Building';

    public function getOptions()
    {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true)
            ->select(array($db->quoteName('title', 'text'), $db->quoteName('alias', 'value')))
            ->from($db->quoteName('#__buildings'));
        $db->setQuery($query);

        return $db->loadObjectList();
    }
}
1
  • @pixx if this answer solves your issue, please award it the green tick. If something isn't quite right or you would like more explanation, please leave a comment under the answer. All questions should progress to a system-recognized resolution.
    – mickmackusa
    Feb 2, 2019 at 0:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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