0

The following module is saving a text, that should be shown in every category page, but the text is not showing anyway at the frontend. Still, not possible to activate WYSIWYG in order to include text in html. What am I still missing or what could be going wrong? Magento 2.1.9.

The module is as follows:

composer.json (app/code/XX/CategoryAttribute)

{
  "name": "xx/categoryattribute",
  "description": "N/A",
  "require": {
    "php": "~5.5.0|~5.6.0|~7.0.0"
  },
  "type": "magento2-module",
  "version": "1.0.0",
  "license": [
    "OSL-3.0",
    "AFL-3.0"
  ],
  "autoload": {
    "files": [ "registration.php" ],
    "psr-4": {
      "XX\\CategoryAttribute\\": ""
    }
  }
}

registration.php (app/code/XX/CategoryAttribute)

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'XX_CategoryAttribute',
    __DIR__
);
?>

module.xml (app/code/XX/CategoryAttribute/etc)

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="XX_CategoryAttribute" setup_version="1.0.0" />
</config>

InstallData.php (app/code/XX/CategoryAttribute/Setup)

<?php

namespace XX\CategoryAttribute\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Catalog\Model\Category;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;

/**
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface
{
    /**
     * @var EavSetupFactory
     */
    private $eavSetupFactory;

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

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(
            Category::ENTITY,
            'custom_attribute',
            [
                'type' => 'text',
                'label' => 'Description',
                'input' => 'textarea',
                'required' => false,
                'sort_order' => 1,
                'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE,
                'used_in_product_listing' => true,
                'group' => 'General Information',
                'maxlength' => 500,
                'wysiwyg_enabled' => true,
                'visible_on_front' => false,
                'is_html_allowed_on_front' => true,
            ]
        );
        $setup->endSetup();
    }
}

category_form.xml (app/code/XX/CategoryAttribute/view/adminhtml/ui_component)

<?xml version="1.0" encoding="UTF-8"?>

<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="custom_content">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Category Description</item>
                <item name="collapsible" xsi:type="boolean">true</item>
                <item name="sortOrder" xsi:type="number">1</item>
            </item>
        </argument>
        <field name="custom_attribute">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="sortOrder" xsi:type="number">10</item>
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="label" xsi:type="string" translate="true">Description</item>
                    <item name="required" xsi:type="boolean">false</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>
0

You should try something following way:


<field name="custom_attribute">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string" translate="true">Custom Attribute</item>
            <item name="class" xsi:type="string">\Magento\Ui\Component\Form\Element\Wysiwyg</item>
            <item name="formElement" xsi:type="string">wysiwyg</item>
            <item name="template" xsi:type="string">ui/form/field</item>
            <item name="source" xsi:type="string">block</item>
            <item name="wysiwyg" xsi:type="boolean">true</item>
            <item name="dataScope" xsi:type="string">custom_attribute</item>
            <item name="rows" xsi:type="number">8</item>
            <item name="sortOrder" xsi:type="number">50</item>
            <item name="validation" xsi:type="array">
                <item name="required-entry" xsi:type="boolean">true</item>
            </item>
        </item>
    </argument>
</field>
  • Hi Sohel... With your code I could activate the wysiwyg editor for including html (thank you!!!), but now I am facing the issue that the text that I save, is not showing in the frontend; what could I still be missing? – Julian Murillo Dec 5 '17 at 18:02
  • You should try like $category->getCustomAttribute(); from frontend – Sohel Rana Dec 6 '17 at 3:00
  • Where exactly? In the category product template it throws an error (undefined variable; which seems logical) – Julian Murillo Dec 6 '17 at 7:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.