3

In articles, I have created a series of custom fields, including two date fields and a checkboxes field. Now, through a plugin in the onContentBeforeSave event I need to verify that if date 1 is less than date 2 then set the value of the checkbox field. For this I try the following

<?php

defined( '_JEXEC' ) or die( 'Restricted access' );

class plgContentAlertcustomfieldhandler extends JPlugin
{
    public function onContentBeforeSave($context, &$article, $isNew, $data)
    {
        if ($context === 'com_content.article') {
            $date1 = strtotime($data['com_fields']['date1']);
            $date2 = strtotime($data['com_fields']['date2']);

            if ($date1 < $date2) {
                $data['com_fields']['alert'][0] = 'alert';
            }
        }

        return true;
    }
}

I try to set the value in this line:

$data['com_fields']['alert'][0] = 'alert';

This custom field has only one checkbox, defined with the name Alert and with the value alert.

Update 1:

I am trying $data['com_fields']['alert'] = true; but I am getting the same result.

Update 2:

If I manually set a value to the checkbox, debugging I can verify that the value assigned to alert in $data is

array (
   0 => 'alert',
)

I try to apply this same value programmatically as follows

$data['com_fields']['alert'] = ['alert'];

But the value is not persisted, at the end of the process of saving and refreshing the view the checkbox remains unchecked

Update 3

Following the process of saving through debugging I identify that if I programmatically assign the value to the custom field through $data ['com_fields']['alert'] = ['alert']; when arriving to the save method in libraries/src/MVC/Model/AdminModel.php in the argument $data the value of alert is false, while if I set a value manually when arriving at this method in$data the value of the alert field is [0 => 'alert'] and consequently the value of the checkbox is assigned.

Update 4

The typo error pointed out by alexandreelise is now repaired. The problem still persists.

Update 5

Using debug I managed to identify that it doesn't matter which field within $ data['com_fields'] I modify in the onContentBeforeSave event once it delegates to the trigger method of the JEventDispatcher class located at libraries/joomla/event/dispatcher.php the argument $args that includes in the index 3 the data to be persisted including those of com_fields retain the values set by the user even if they have been programmatically modified as in the example I described. Eventually this data is passed to libraries/src/MVC/Model/AdminModel.php in the save method that has the argument $data the data to be inserted preserves the values set by the user. Right now I think that maybe the solution is to try to modify these values in another event maybe before onContentBeforeSave. Thanks again for any ideas

I appreciate your advice

2
  • Hi, Is there a particular reason why you would use checkboxes for only one checkbox? Maybe you should use a radio button for that. Just my 2 cents piece of advise. Commented Oct 30, 2019 at 2:44
  • for now it's the requirement that I have
    – user615274
    Commented Oct 30, 2019 at 3:20

1 Answer 1

3

Basically you needed to use onContentAfterSave rather then onContentBeforeSave and use the FieldsHelper in conjuction with the FieldsModelField.

Here is the code:

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

class plgContentAlertcustomfieldhandler extends JPlugin
{
   public function onContentAfterSave($context, &$article, $isNew)
   {

    if ($context === 'com_content.article') {
        $custom_fields = FieldsHelper::getFields($context, $article, true);
        $custom_fields_by_name = \Joomla\Utilities\ArrayHelper::pivot($custom_fields, 'name');

        $date1 = strtotime($custom_fields_by_name['date1']->value);
        $date2 = strtotime($custom_fields_by_name['date2']->value);

        if ($date1 < $date2) {
            //define the value you want to assign
            $your_custom_field_value = 'alert';

            JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_fields/models');
            /**
             * @var FieldsModelField $model_field
             */
            $model_field = JModelLegacy::getInstance('Field', 'FieldsModel', ['ignore_request' => true]);

            //set the value using field model instead to make change permanent in db
            $model_field->setFieldValue(
                $custom_fields_by_name['alert']->id,
                $article->id,
                $your_custom_field_value
            );
        }
    }

     return true;
     }
  }

UPDATE:

Second version of the code toggling on and off the checkbox when your requirement is not met.

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

class plgContentAlertcustomfieldhandler extends JPlugin
{
    public function onContentAfterSave($context, &$article, $isNew)
    {

    if ($context === 'com_content.article') {
        $custom_fields = FieldsHelper::getFields($context, $article, true);
        $custom_fields_by_name = \Joomla\Utilities\ArrayHelper::pivot($custom_fields, 'name');

        $date1 = strtotime($custom_fields_by_name['date1']->value);
        $date2 = strtotime($custom_fields_by_name['date2']->value);

        $your_custom_field_value = '';
        if ($date1 < $date2) {
            //define the value you want to assign
            $your_custom_field_value = 'alert';
        }
        JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_fields/models');
        /**
         * @var FieldsModelField $model_field
         */
        $model_field = JModelLegacy::getInstance('Field', 'FieldsModel', ['ignore_request' => true]);

        //set the value using field model instead to make change permanent in db
        $model_field->setFieldValue(
            $custom_fields_by_name['alert']->id,
            $article->id,
            $your_custom_field_value
        );
      }

      return true;
     }
 }
0

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.