3

I have added a custom field in article creation page but it gets displayed in-between the option tab but I need the field to be displayed between Title and alias field.

Here is my code which I did refering https://docs.joomla.org/Adding_custom_fields_to_the_article_component

Rating.php

<?php
//$xml = simplexml_load_file("multiurlredirect.xml");

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

jimport( 'joomla.plugin.plugin' );
jimport( 'joomla.form.form' );


class plgsystemrating extends JPlugin
{

   public function __construct(& $subject, $config)
   {
      parent::__construct($subject, $config);

   }

   public function onContentPrepareForm($form, $data)
   { 

      if (!($form instanceof JForm))
      {
         $this->_subject->setError('JERROR_NOT_A_FORM');
         return false;
      }

      JFormHelper::addFormPath(dirname(__FILE__) . '/rating');

      $form->loadFile('rating', true);

      return true; 
   }
}

rating.xml

  <config>
     <fields name="params">
        <fieldset name="primaryfieldset">
           <field
              name="ratingclass_sfx"
              type="text"
              label="Rating Class"
              description="Optional CSS class to add to the rating. This allows CSS styling specific to the page."
           />
        </fieldset>
     </fields>
  </config>

rating/rating.xml

  <fields name="attribs">
     <fieldset name="primaryfieldset" label="Rating Options">
        <field
           name="texture"
           type="text"
           id="texture"
           description="What does the sample feel like"
           label="Texture"
           message="PLG_CONTENT_RATING_FIELD_TEXTURE_MESSAGE"
           size="30"
        />
     </fieldset>
  </fields>

And my Output enter image description here

I need the Rating option to be displayed in between the title and alias field.

Please any one explain me how to do this

2

If you look at the template file at administrator/components/com_content/views/article/tmpl/edit.php on line 78 you'll see the following code:

<?php echo JLayoutHelper::render('joomla.edit.title_alias', $this); ?>

Which contains the following code to render the title and alias for the article:

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

$form = $displayData->getForm();

$title = $form->getField('title') ? 'title' : ($form->getField('name') ? 'name' : '');

?>
<div class="form-inline form-inline-header">
    <?php
    echo $title ? $form->renderField($title) : '';
    echo $form->renderField('alias');
    ?>
</div>

What you want is to perform a layout override on the title_alias.php by copying the file into {your_template}/html/layouts/joomla/edit/ folder. Once copied, edit the file by adding your custom field.

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

$form = $displayData->getForm();

$title = $form->getField('title') ? 'title' : ($form->getField('name') ? 'name' : '');

?>
<div class="form-inline form-inline-header">
    <?php
    echo $title ? $form->renderField($title) : '';
    echo $form->renderField('my_custom_field');  <--- NEW LINE
    echo $form->renderField('alias');
    ?>
</div>

This should place the field where you need it. One thing you will have to do is alter your XML file so that it isn't automatically rendered as a tab. You should be able to do this by embedding the directly to the instead of inside a and .

Hope this helps.

Joomla Output Overrides

Joomla Layout Overrides

  • Thanks Brain it works but if need to do this through a plugin what i have to do – manikandan palanisamy Mar 25 '16 at 4:25
  • If you want to do it exclusively in the plugin your only option would be on the events, like system onAfterRender where you could access a string representation of the DOM, use DOMDocument class to create an object representation of the to be rendered page and step through DOM manually to insert required HTML in proper location. This solution would be EXTREMELY fragile however, as any Joomla core updates with changes the HTML taxonomy of the back-end article edit page, which could be practically every release! – Brian Bolli Mar 25 '16 at 18:53
  • Other idea would be to the $form object, after you load ur custom XML, to alter the location. You won't be able to place between title and alias, but there are some parts which will auto-render fields. This is how the new tab and field were automatically added in your original example. Using this approach you could embed into one of the other existing tabs and field grouping by moving your custom field from on fieldset to another programmatically. – Brian Bolli Mar 25 '16 at 18:55

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.