Якщо вам потрібно розширити модуль блогу Magento 2 від Magefan та додати нове текстове поле на сторінку редагування дописів блогу в адміністративній панелі Magento 2 (Вміст > Блог > Дописи), вам потрібно створити просте власне розширення Magento 2.
Ви можете завантажити зразок розширення з нашої сторінки GitHub або створити його вручну, виконавши наведені нижче кроки:
1. Створіть файл composer.json
app/code/Magefan/BlogCustomField/composer.json
{
"name": "magefan/module-blog-custom-field",
"description": "Implements Blog functionality on Magento 2 store",
"require": {
"magefan/module-blog" : ">=2.8.0"
},
"type": "magento2-module",
"version": "2.0.0",
"license": [
"OSL-3.0",
"AFL-3.0"
],
"autoload": {
"files": [ "registration.php" ],
"psr-4": {
"Magefan\\BlogCustomField\\": ""
}
}
}
2. Створіть файл registration.php
app/code/Magefan/BlogCustomField/registration.php
<?php
/**
* Copyright © Magefan (support@magefan.com). All rights reserved.
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
*/
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Magefan_BlogCustomField',
__DIR__
);
3. Створіть файл module.xml
app/code/Magefan/BlogCustomField/etc/module.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © Magefan (support@magefan.com). All rights reserved.
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magefan_BlogCustomField" setup_version="2.0.0">
<sequence>
<module name="Magefan_Blog"/>
</sequence>
</module>
</config>
4. Створіть файл InstallSchema.php
app/code/Magefan/BlogCustomField/Setup/InstallSchema.php
<?php
/**
* Copyright © Magefan (support@magefan.com). All rights reserved.
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
*/
namespace Magefan\BlogCustomField\Setup;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\DB\Adapter\AdapterInterface;
/**
* Blog setup
*/
class InstallSchema implements InstallSchemaInterface
{
/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
/* Add one custom text field */
$table = $setup->getTable('magefan_blog_post');
$setup->getConnection()->addColumn(
$setup->getTable($table),
'custom_field',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 255,
'nullable' => true,
'comment' => 'Custom Field',
]
);
$setup->endSetup();
}
}
5. Створіть файл blog_post_form.xml
app/code/Magefan/BlogCustomField/view/adminhtml/ui_component/blog_post_form.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magefan (support@magefan.com). All rights reserved.
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
*/
-->
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<fieldset name="additional_options">
<field name="title">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="dataType" xsi:type="string">text</item>
<item name="label" xsi:type="string" translate="true">Custom Field</item>
<item name="formElement" xsi:type="string">input</item>
<item name="source" xsi:type="string">post</item>
<item name="sortOrder" xsi:type="number">999</item>
<item name="dataScope" xsi:type="string">custom_field</item>
<!--
<item name="validation" xsi:type="array">
<item name="required-entry" xsi:type="boolean">true</item>
</item>
-->
</item>
</argument>
</field>
</fieldset>
</form>
Після створення всіх файлів розширення виконайте команди CLI для встановлення розширення:
php bin/magento setup:upgrade
php bin/magento cache:clean
Перейдіть до панелі адміністратора Magento 2 > Контент > Блог > Дописи > Редагувати допис. Ви побачите нове поле.
![]()
Щоб відобразити значення цього поля на вітрині магазину, використовуйте код
<?= $post->getData('custom_field') ?>
. Якщо ви додасте цей код до файлу шаблону view/frontend/templates/post/view.phtml, наприклад:
<?php if ($_post->getData('custom_field')) { ?>
<h2><?= $block->escapeHtml($_post->getData('custom_field')) ?></h2>
<?php } ?>
, ви побачите
![]()
Unfortunately, we don't have a guide for this.
When you submit a request with multi-select parameter - Magento receives it as an array, and in a database, you use varchar or text field to save it, so before save you need to convert array to string (e.g. implode PHP function using comma), and afterload convert string to array (e.g. explode function using comma). To add your code on post save/load, you can use Magento interceptors or observers.