Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add complete example module for a (simple) configuration page for a module #158

Merged
merged 9 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions demosymfonyformsimple/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Demonstration of how to use Symfony form types for Module configuration pages

## About

This module demonstrates how to use Symfony form types for Module configuration pages.

[It is the module created by this complete guide on devdocs](https://devdocs.prestashop-project.org/8/modules/creation/adding-configuration-page-modern/).

It provides a simple configuration page for a module, with a Text Field.
thomasnares marked this conversation as resolved.
Show resolved Hide resolved

## Supported PrestaShop versions

This module is compatible with PS 8.0.x versions only.
thomasnares marked this conversation as resolved.
Show resolved Hide resolved

## Requirements

Composer

## How to install

Download or clone module into modules directory of your PrestaShop installation
thomasnares marked this conversation as resolved.
Show resolved Hide resolved
Rename the directory to make sure that module directory is named demosymfonyformsimple
thomasnares marked this conversation as resolved.
Show resolved Hide resolved

`cd` into module's directory and run following commands:

`composer install` - to download dependencies into vendor folder

Install module from Back Office or via CLI.
thomasnares marked this conversation as resolved.
Show resolved Hide resolved
23 changes: 23 additions & 0 deletions demosymfonyformsimple/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "prestashop/demosymfonyformsimple",
"description": "PrestaShop - Settings Form Examples",
"license": "AFL-3.0",
"authors": [
{
"name": "PrestaShop Core Team"
}
],
"autoload": {
"psr-4": {
"PrestaShop\\Module\\DemoSymfonyFormSimple\\": "src/"
}
},
"require": {
"php": ">=7.1.0"
thomasnares marked this conversation as resolved.
Show resolved Hide resolved
},
"config": {
"preferred-install": "dist",
"prepend-autoloader": false
},
"type": "prestashop-module"
}
8 changes: 8 additions & 0 deletions demosymfonyformsimple/config/routes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
demo_configuration_form_simple:
path: /demosymfonyformsimple/configuration
methods: [GET, POST]
defaults:
_controller: 'PrestaShop\Module\DemoSymfonyFormSimple\Controller\DemoConfigurationController::index'
# Needed to work with tab system
_legacy_controller: AdminDemoSymfonyFormSimple
_legacy_link: AdminDemoSymfonyFormSimple
29 changes: 29 additions & 0 deletions demosymfonyformsimple/config/services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
services:
_defaults:
public: true

# Demo configuration text form
prestashop.module.demosymfonyformsimple.form.type.demo_configuration_text:
class: 'PrestaShop\Module\DemoSymfonyFormSimple\Form\DemoConfigurationFormType'
parent: 'form.type.translatable.aware'
public: true
tags:
- { name: form.type }

prestashop.module.demosymfonyformsimple.form.demo_configuration_text_data_configuration:
class: PrestaShop\Module\DemoSymfonyFormSimple\Form\DemoConfigurationTextDataConfiguration
arguments: ['@prestashop.adapter.legacy.configuration']

prestashop.module.demosymfonyformsimple.form.demo_configuration_text_form_data_provider:
class: 'PrestaShop\Module\DemoSymfonyFormSimple\Form\DemoConfigurationTextFormDataProvider'
arguments:
- '@prestashop.module.demosymfonyformsimple.form.demo_configuration_text_data_configuration'

prestashop.module.demosymfonyformsimple.form.demo_configuration_text_form_data_handler:
class: 'PrestaShop\PrestaShop\Core\Form\Handler'
arguments:
- '@form.factory'
- '@prestashop.core.hook.dispatcher'
- '@prestashop.module.demosymfonyformsimple.form.demo_configuration_text_form_data_provider'
- 'PrestaShop\Module\DemoSymfonyFormSimple\Form\DemoConfigurationFormType'
- 'DemoConfiguration'
34 changes: 34 additions & 0 deletions demosymfonyformsimple/demosymfonyformsimple.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

use PrestaShop\PrestaShop\Adapter\SymfonyContainer;

class DemoSymfonyFormSimple extends Module
{
public function __construct()
{
$this->name = 'demosymfonyformsimple';
$this->author = 'PrestaShop';
$this->version = '1.1.0';
thomasnares marked this conversation as resolved.
Show resolved Hide resolved
$this->need_instance = 0;

$this->bootstrap = true;
parent::__construct();

$this->displayName = $this->trans('Demo symfony form configuration simple', [], 'Modules.DemoSymfonyFormSimple.Admin');
thomasnares marked this conversation as resolved.
Show resolved Hide resolved
$this->description = $this->trans(
'Module created for the purpose of showing existing form types within PrestaShop',
thomasnares marked this conversation as resolved.
Show resolved Hide resolved
[],
'Modules.DemoSymfonyFormSimple.Admin'
);

$this->ps_versions_compliancy = ['min' => '8.0.0', 'max' => '8.99.99'];
}

public function getContent()
{
$route = SymfonyContainer::getInstance()->get('router')->generate('demo_configuration_form_simple');
Tools::redirectAdmin($route);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace PrestaShop\Module\DemoSymfonyFormSimple\Controller;

use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class DemoConfigurationController extends FrameworkBundleAdminController
{
public function index(Request $request): Response
{
$textFormDataHandler = $this->get('prestashop.module.demosymfonyformsimple.form.demo_configuration_text_form_data_handler');

$textForm = $textFormDataHandler->getForm();
$textForm->handleRequest($request);

if ($textForm->isSubmitted() && $textForm->isValid()) {
/** You can return array of errors in form handler and they can be displayed to user with flashErrors */
$errors = $textFormDataHandler->save($textForm->getData());

if (empty($errors)) {
$this->addFlash('success', $this->trans('Successful update.', 'Admin.Notifications.Success'));

return $this->redirectToRoute('demo_configuration_form_simple');
}

$this->flashErrors($errors);
}

return $this->render('@Modules/demosymfonyformsimple/views/templates/admin/form.html.twig', [
'demoConfigurationForm' => $textForm->createView(),
]);
}
}
22 changes: 22 additions & 0 deletions demosymfonyformsimple/src/Form/DemoConfigurationFormType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);

namespace PrestaShop\Module\DemoSymfonyFormSimple\Form;

use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use PrestaShopBundle\Form\Admin\Type\TranslatorAwareType;

class DemoConfigurationFormType extends TranslatorAwareType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('config_text', TextType::class, [
'label' => $this->trans('Configuration text', 'Modules.DemoSymfonyFormSimple.Admin'),
thomasnares marked this conversation as resolved.
Show resolved Hide resolved
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @author PrestaShop SA <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/

declare(strict_types=1);

namespace PrestaShop\Module\DemoSymfonyFormSimple\Form;

use PrestaShop\PrestaShop\Core\Configuration\DataConfigurationInterface;
use PrestaShop\PrestaShop\Core\ConfigurationInterface;

/**
* Configuration is used to save data to configuration table and retrieve from it
*/
final class DemoConfigurationTextDataConfiguration implements DataConfigurationInterface
{
public const DEMO_SYMFONY_FORM_SIMPLE_TEXT_TYPE = 'DEMO_SYMFONY_FORM_SIMPLE_TEXT_TYPE';

/**
* @var ConfigurationInterface
*/
private $configuration;

/**
* @param ConfigurationInterface $configuration
*/
public function __construct(ConfigurationInterface $configuration)
{
$this->configuration = $configuration;
}

/**
* {@inheritdoc}
*/
public function getConfiguration(): array
{
$return = [];

if ($textSimple = $this->configuration->get(static::DEMO_SYMFONY_FORM_SIMPLE_TEXT_TYPE)) {
$return['config_text'] = $textSimple;
}

return $return;
}

/**
* {@inheritdoc}
*/
public function updateConfiguration(array $configuration): array
{
$this->configuration->set(static::DEMO_SYMFONY_FORM_SIMPLE_TEXT_TYPE, $configuration['config_text']);

/* Errors are returned here. */
return [];
}

/**
* Ensure the parameters passed are valid.
*
* @param array $configuration
*
* @return bool Returns true if no exception are thrown
*/
public function validateConfiguration(array $configuration): bool
{
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);

namespace PrestaShop\Module\DemoSymfonyFormSimple\Form;

use PrestaShop\PrestaShop\Core\Configuration\DataConfigurationInterface;
use PrestaShop\PrestaShop\Core\Form\FormDataProviderInterface;

/**
* Provider ir responsible for providing form data, in this case it's as simple as using configuration to do that
*
* Class DemoConfigurationTextFormDataProvider
*/
class DemoConfigurationTextFormDataProvider implements FormDataProviderInterface
{
/**
* @var DataConfigurationInterface
*/
private $demoConfigurationTextDataConfiguration;

/**
* @param DataConfigurationInterface $demoConfigurationTextDataConfiguration
*/
public function __construct(DataConfigurationInterface $demoConfigurationTextDataConfiguration)
{
$this->demoConfigurationTextDataConfiguration = $demoConfigurationTextDataConfiguration;
}

/**
* {@inheritdoc}
*/
public function getData(): array
{
return $this->demoConfigurationTextDataConfiguration->getConfiguration();
}

/**
* {@inheritdoc}
*/
public function setData(array $data): array
{
return $this->demoConfigurationTextDataConfiguration->updateConfiguration($data);
}
}
26 changes: 26 additions & 0 deletions demosymfonyformsimple/views/templates/admin/form.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% extends '@PrestaShop/Admin/layout.html.twig' %}

{# PrestaShop made some modifications to the way forms are displayed to work well with PrestaShop in order to benefit from those you need to use ui kit as theme#}
{% form_theme demoConfigurationForm '@PrestaShop/Admin/TwigTemplateForm/prestashop_ui_kit.html.twig' %}

{% block content %}
{{ form_start(demoConfigurationForm) }}
<div class="card">
<h3 class="card-header">
<i class="material-icons">settings</i> {{ 'Text form types'|trans({}, 'Modules.DemoSymfonyFormSimple.Admin') }}
</h3>
<div class="card-body">
<div class="form-wrapper">
{{ form_widget(demoConfigurationForm) }}
</div>
</div>
<div class="card-footer">
<div class="d-flex justify-content-end">
<button class="btn btn-primary float-right" id="save-button">
{{ 'Save'|trans({}, 'Admin.Actions') }}
</button>
</div>
</div>
</div>
{{ form_end(demoConfigurationForm) }}
{% endblock %}
thomasnares marked this conversation as resolved.
Show resolved Hide resolved