[solved] Checking/modifying fields after item form submit

Hello,

For multilingual reasons, I need to check if some metadata fields have two or more textareas.

Like this :
exemple_bilingue

And in the case there is not enough textareas in that field, I still want to save the item but not allow it to be public. Then I want to display a flash error message telling the invalid fields.

I’m thinking about using the before_save_item hook but I have no idea what to do next.
https://omeka.readthedocs.io/en/latest/Reference/hooks/before_save_<model>.html

I understand that I can get the POST data or the record but then, do I have to return something ?

And for the flash message, AbstractPlugin doesn’t seem to have a _helper that allows redirect and flash messages, how can I get these functions outside of controllers ?

Sorry if I’m asking a lot but thanks in advance

I’m slowly getting somewhere …

I tried this in my plugin to see the behaviour :

     public function hookBeforeSaveItem($args) {
        $title = metadata($args['record'], array('Dublin Core', 'Title'), 'all');

        if (count($title) == 2) {
            $args['record']->public = 1;
        }
    }

The item becomes public if there a two fields for Title and not when there are three or more, but … an exception is thrown when I remove a field. I guess it’s because metadata() lock the field while I use it.

How can I prevent this ? Is there a better way ?

exemple_delete_elementtext

To learn Omeka, you can check the documentation and existing plugins that use this hook.

If you save the record inside the hook used when the record is saved, you will have an infinite loop. So just set the metadata, the record is an object and is automatically saved.

        $record = $args['record'];
        $view = get_view();
        $titles = $record->getElementTexts('Dublin Core', 'Title');
        if (count($titles) == 2) {
            $record->public = 1;
        }

About flash :

        } else {
            $message = __('Vous devez indiquez plusieurs titres.');
            $flash = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
            $flash->addMessage($message, 'error');
        }     

Thanks for your guidance. :+1:

But now I’m facing another problem :

In BeforeSave, the record we are modifying still doesn’t have the changes in the form (if the user added a field, it won’t detect it).
And in AfterSave, the record is up to date but we can’t change the public attribute anymore …

This kinda works but not really because if I am going to edit an item to add a field, it will say that I miss a field even though I added one in the form. :confused:

 public function hookBeforeSaveItem($args) {
        $flash = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
        $record = $args['record'];
        $titles = $record->getElementTexts('Dublin Core', 'Title');

        if (is_array($titles) && count($titles) < 2) {
            $message = __("This item can't be public because it needs additional titles.");
            $flash->addMessage($message, 'error');
            $args['record']->public = 0;
        }
    }

Unlike Omeka S, when the user clicks Save, there is no way to stop the saving process, so the user is warned at least.

Oh ok then I’ll settle for prevention :slight_smile:

Thank you for your help !