3

In Magento 2 I've added a custom attribute to CMS pages (another post on here got me part of the way).

I used UpgradeSchema.php to create the new attribute and I can see my new attribute/column in the cms_page table. I also created an Observer to add the field to CMS pages in Admin, and the field works (changes are saved) in the Admin > Content > Pages > Content tab.

This is where I've got stuck. I've got this far by piecing together bits and pieces of information I could find, but I can't find anything on how to actually 'get' this data and output it on the frontend.

At the moment I have an (empty) Helper, and a phtml template which is ready to output the value in. But I don't know what to put in the Helper, or even if that is the right thing to do (should I be extending the Page or Block class from example)?

As a lowly frontend developer trying to muddle his way through a Magento world, I could do with a little nudge in the right direction. Thanks.

2 Answers 2

1

Based on Khoa's example above I was able to get things working.

Here's my Helper:

// app/code/VendorName/ModuleName/Helper/Data.php

namespace VendorName\ModuleName\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;

class Data extends AbstractHelper
{
  protected $_page;
  protected $_pageFactory;

  public function __construct(
    \Magento\Cms\Model\Page $page,
    \Magento\Cms\Model\PageFactory $pageFactory,
    Context $context,

    array $data = []
  )
  {
    $this->_page = $page;
    $this->_pageFactory = $pageFactory;
    parent::__construct($context, $data);
  }

  public function getCmsPageId() {
    return $this->_page->getId();
  }

  public function getMyCustomAttribute($id)
  {
    $page = $this->_pageFactory->create()->load($id);
    return $page->getData('my_custom_attribute');
  }
}

And then in my template:

$cmsPageId = $this->helper('VendorName\ModuleName\Helper\Data')->getCmsPageId();
$myCustomAttribute = $this->helper('VendorName\ModuleName\Helper\Data')->getMyCustomAttribute($cmsPageId);

This puts my custom attribute into the $myCustomAttribute variable, ready for me to do what I want with it.

I don't know if this is the best way to achieve what I want, but it works.

0

We should have a construction to inject Magento\Cms\Api\PageRepositoryInterface. For example, the helper likes:

app/code/Vendor/Cms/Helper/Data.php

namespace Vendor\Cms\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;

class Data extends AbstractHelper
{
    protected $pageRepository;

    public function __construct(
       \Magento\Cms\Api\PageRepositoryInterface $pageRepository,
        Context $context
    )
    {
        $this->pageRepository = $pageRepository;
        parent::__construct($context);
    }

    public function getCustomAttribute($id)
    {
        $page = $this->pageRepository->getById($id);
        return $page->getData('your_attribute');
    }
}
2
  • This is great, thank you. I added a little more code to get the current page ID to pass into the getPage() method, and I discovered that the line where you're using load() is missing a preceeding create() method. However, it gave me enough info to get things working! I'm going to post my actual code below for anyone else who is interested.
    – Matt
    Commented Jun 30, 2016 at 20:49
  • I updated my code. Commented Jun 30, 2016 at 23:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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