3

I created a plugin to add custom fields to the joomla com_content core. I add the fields to the backend and frontend. Now the new tab is shown on every post but i want to show the tab and the fields just for one specific category (id=43).

This is how i add the new form to the backend and frontend:

<?php
// no direct access
defined ( '_JEXEC' ) or die ( 'Restricted access' );

class plgContentComputerverzeichnis extends JPlugin {

        function onContentPrepareForm($form, $data) {

                $app = JFactory::getApplication();
                $option = $app->input->get('option');

                switch($option) {

                    case 'com_content':
                        if ($app->isAdmin()) { 
                            JForm::addFormPath(__DIR__ . '/forms');
                            $form->loadFile('content', false); 
                        }
                    case 'com_content':
                        if ($app->isSite()) {
                            JForm::addFormPath(__DIR__ . '/forms');
                            $form->loadFile('content', false);
                        }
                    return true;

                }
                return true;
        }

}
?>  

And this is my form:

<?xml version="1.0" encoding="UTF-8"?>
<form>
    <fields name="attribs" >
        <fieldset name="custom" label="Lizenzinformationen">

            <field name="typ" type="list" label="Typ">
                <option value="-">Wähle einen Typ aus!</option>  
                <option value="1">Mac</option>  
                <option value="2">PC</option>      
                <option value="3">Server</option>      
            </field>

            <field
                name="arbeitsplatz"
                type="sql"
                label="Arbeitsplatz"
                query="SELECT id, name FROM #__users"
                key_field="id" 
                value_field="name"
                header="Wähle einen Arbeitsplatz aus"
                showon="typ:1[OR]typ:2"
                >
            </field>

            <field name="computerversion"
                type="text"
                label="Computerversion"
                showon="typ:1[OR]typ:2[OR]typ:3"
            />

            <field name="notizen" type="editor" label="Notizen" description="" width="300" filter="safehtml" showon="typ:1[OR]typ:2[OR]typ:3" />

        </fieldset>
    </fields>
</form>
3
  • Since Joomla 3.7 we have the Custom Fields, that we use in a similar way to your requirement, so all the effort of writing a plugin may be no longer be necessary. docs.joomla.org/J3.x:Adding_custom_fields Commented Jun 21, 2017 at 16:15
  • I know the new custom fields in joomla 3.7. But is it possible to add showon values to these fields? I dont think so.
    – public9nf
    Commented Jun 22, 2017 at 7:40
  • I don't understand the expression "showon values". I found custom fields quite flexible, being able to group them, and assign groups to different categories, but you can also address the display by overriding your views if you need to achieve more sophisticated results. Commented Jun 23, 2017 at 8:23

2 Answers 2

3

I now created a field inside the plugin where i can select the categories where the fields are shown. In my plugin i select these categories and just show the form when the post belong to one of these categories. This works, but now when i save the post. The changes are not been saved:

<?php
// no direct access
defined ( '_JEXEC' ) or die ( 'Restricted access' );

class plgContentComputerverzeichnis extends JPlugin {

        function onContentPrepareForm($form, $data) {

                $app = JFactory::getApplication();
                $option = $app->input->get('option');
                $include_categories = $this->params->get('include_categories');

                switch($option) {

                    case 'com_content':
                        if ($app->isAdmin()) { 
                            JForm::addFormPath(__DIR__ . '/forms');
                            $form->loadFile('content', false);
                        }
                    case 'com_content':
                        if ($app->isSite()) {
                            if (in_array($data->catid, $include_categories)) {
                                JForm::addFormPath(__DIR__ . '/forms');
                            } 
                            $form->loadFile('content', false);
                        }
                    return true;      
                }
            return true;
        }

}
?>
2
  • If this does not solve your question, then it should be posted as an update on your question. Commented Feb 3, 2019 at 2:41
  • Or is this a working solution within the scope of the question. Did you post a separate question to address the new issue? Commented Feb 3, 2019 at 2:43
1

In your form XML try replacing custom with basic

This

    <fieldset name="custom" label="Lizenzinformationen">

change to this

    <fieldset name="basic" label="Lizenzinformationen">

Your Answer

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

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