Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Plugins

Create Flex Object with FE Form

form

Solved by pamtbaau View solution

Started by Thomas Bieli 4 years ago · 3 replies · 739 views
4 years ago

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

4 years ago Solution

@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.

<details>
<summary>Example Form definition in page</summary>

YAML
  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

</details>

  • 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.

<details>
<summary>Example custom Plugin</summary>

PHP
  <?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;
      }
    }
  }

</details>

👍 1
last edited 08/05/22 by pamtbaau
4 years ago

@Karmalakas yes front-end form

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

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 54 1 week ago
Plugins · by Xavier, 4 weeks ago
2 61 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1188 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 54 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 79 2 months ago