Якщо вам потрібно розширити Magento 2 Блог від Magefan і додати нове кастомне текстове поле на сторінку редагування публікації блогу в адмін панелі Magento 2 (Content > Blog > Posts), ви повинні створити просте кастомне розширення 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
/**
* 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








4. Створіть файл InstallSchema.php 

app/code/Magefan/BlogCustomField/Setup/InstallSchema.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

 








text
Custom Field
input
post
999
custom_field





Після створення всіх файлів розширень, запустіть CLI команди, щоб встановити розширення:

php bin/magento setup:upgrade
php bin/magento cache:clean

Підіть в Admin Panel > Content > Blog > Posts > Edit Post. Ви побачите нове поле.

Magento 2 Blog Module Custom Field

Щоб відобразити значення цього поля на вітрині, використовуйте наступний код:

getData('custom_field') ?>

Якщо ви додасте цей код до шаблону файлу view/frontend/templates/post/view.phtml, напр.:

getData('custom_field')) { ?>
   

escapeHtml($_post->getData('custom_field')) ?>


ви побачите наступне: 

Magento 2 Blog Extension Custom Field Example