Create Flex Object with FE Form

Hi

I wan’t to create Flex Object with an FE Form. Is this possible?
Is there maybe an process (or is it possible with de save process) for the form plugin.
Or any better idea?

Thanks
Thomas

What is this FE Form? :thinking:

@Thoomyy, I presume an FE form is a front-end form and not Admin…

Disclaimer: I have no experience with Flex-Objects and the following is based on the docs I’ve just read and a bit of playing with code.

Reading the docs, I haven’t come across a way to create a form that automatically saves the form into a Flex Directory.

However, using a regular form in a page and a custom plugin, it can be done as follows:

  • Front-end:

    • Create an ordinary form in a page containing the fields of your object and a custom action.
    Example Form definition in page
    form:
      name: contact
      fields:
        published:
          type: toggle
          label: PLUGIN_ADMIN.ENABLED
          highlight: 1
          help: PLUGIN_ADMIN.ENABLED_HELP
          options:
            1: PLUGIN_ADMIN.YES
            0: PLUGIN_ADMIN.NO
          validate:
            type: bool
        first_name: 
          type: text
          validate:
            required: true
        last_name:
          type: text
          validate:
            required: true
        email:
          type: email
          validate:
            required: true
        website:
          type: text
          validate:
            required: true
        tags:
          type: select
          multiple: true
          options:
            Red: Red
            Blue: Blue
            Green: Green
            White: White
            Black: Black
            Pink: Pink
            Violet: Violet
          validate:
            type: array
            
      buttons:
        submit:
          type: submit
      process:
        - addContact:
        - reset: true
    
  • Backend

    • In a custom plugin, catch the form being saved in event onFormProcessed,
    • Check for the custom action and use Flex-Object classes to create a new flex-object using the form values being submitted.
    Example custom Plugin
    <?php
    
    namespace Grav\Plugin;
    
    use Composer\Autoload\ClassLoader;
    use Grav\Common\Grav;
    use Grav\Common\Plugin;
    use Grav\Framework\Flex\Interfaces\FlexDirectoryInterface;
    use Grav\Framework\Flex\Interfaces\FlexObjectInterface;
    use Grav\Plugin\Form\Form;
    use RocketTheme\Toolbox\Event\Event;
    
    /**
     * Class ContactPlugin
     * @package Grav\Plugin
     */
    class ContactPlugin extends Plugin
    {
      /**
       * @return array
       *
       * The getSubscribedEvents() gives the core a list of events
       *     that the plugin wants to listen to. The key of each
       *     array section is the event that the plugin listens to
       *     and the value (in the form of an array) contains the
       *     callable (or function) as well as the priority. The
       *     higher the number the higher the priority.
       */
      public static function getSubscribedEvents(): array
      {
        return [
          'onPluginsInitialized' => [
            // Uncomment following line when plugin requires Grav < 1.7
            // ['autoload', 100000],
            ['onPluginsInitialized', 0]
          ]
        ];
      }
    
      /**
       * Composer autoload
       *
       * @return ClassLoader
       */
      public function autoload(): ClassLoader
      {
        return require __DIR__ . '/vendor/autoload.php';
      }
    
      /**
       * Initialize the plugin
       */
      public function onPluginsInitialized(): void
      {
        // Don't proceed if we are in the admin plugin
        if ($this->isAdmin()) {
          return;
        }
    
        // Enable the main events we are interested in
        $this->enable([
          // Put your main even'ts here
          'onFormProcessed' => ['onFormProcessed', 0],
        ]);
      }
    
      public function onFormProcessed(Event $event)
      {
        /** @var Form */
        $form = $event['form'];
        /** @var string */
        $action = $event['action'];
    
        switch ($action) {
          case 'addContact':
            /** @var FlexDirectoryInterface */
            $directory = Grav::instance()->get('flex')->getDirectory('contacts');
    
            /** @var FlexObjectInterface */
            $newObject = $directory->createObject([
              'published' =>  $form->value('published', true),
              'first_name' => $form->value('first_name'),
              'last_name' => $form->value('last_name'),
              'email' => $form->value('email'),
              'website' => $form->value('website'),
              'tags' => $form->value('tags', []),
            ]);
    
            $newObject->save();
    
            break;
          case 'otherAction':
            // ...
            break;
        }
      }
    }
    

@Karmalakas yes front-end form

@pamtbaau thx - it works fine.
I was hoping that flex object already has a general form process (if possible…)