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?

  • 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 '18 at 22:42
0

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();
    }
}
  • @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 at 0:19

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.