1

I've got 2 directories on my root:

/images/ <-- with user's images
/media/  <-- with system ( and components ) images

The global parameter for image root is set to media but I want to decide via the field where to find my image.

The XML for the admin form is

<field
    id="contest_image"
    name="contest_image"
    type="NewMedia"
    required="false"
    imgroot="images"
    directory=""
    label="Contest_image"
    description="contest_image_Desc"
    preview="true"
    exclude="" 
    stripext="0" />

I've duplicated the original field in NewMedia for avoiding confusion and set this one with:

if ($this->value && file_exists(JPATH_ROOT . '/' . $this->value))
{
    $folder = $this->imgroot . '/'.$this->directory;
}
elseif (file_exists(JPATH_ROOT . '/' . $this->imgroot . '/' . $this->directory))
{
    $folder = $this->imgroot . '/'.$this->directory;
}
else
{
    //$folder = '';
    $folder = $this->imgroot ;
}

Can anyone help me ?

1

In your Model, you have to override getForm method to set values dynamically. Make sure you are extending your Model with either JModelForm or JModelAdmin. Once done, try following code:

public function getForm($data = array(), $loadData = true)
{
    $form = $this->loadForm('com_yourcomponent.yourformname', 'yourformname', array('control' => 'jform', 'load_data' => $loadData));

    if (empty($form))
    {
        return false;
    }

    if (condition)
    {
        $form->setFieldAttribute('contest_image', 'directory', 'Your Value if condition is true');
    }
    else
    {
        $form->setFieldAttribute('contest_image', 'directory', 'Your Value is false');
    }

    return $form;
}

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.