Contentful Widgets for Magento 2


License
MIT

Documentation

RomaTech Magento 2 Contentful

A custom Magento 2 module that provides widgets and modules for Magento 2 that uses Contentful as the CMS backend.

The connection to Contful uses the standard Contentful PHP extension and made available as a read only ResourceModel as an extended AbstractDB. As of Magento 2.3 this is the most straightforward way to introduce a custom data source, even thouggh Contenful would not normaly be viewed as a database. One advantage of this approach is that Magento caching is available fo rthe retrieved data.

Data is retreived from Contentful for each Magento Model by model entity with each Magento model 'aligned' to a custom widget/module to allow data to be viwed on the Magento frontend.

Installation

The Contentful custom module requires a fully running Magento 2.3 environment and is installed by copying the cloned repository into the <magento-home>/app/code/RTech/Contact directory.The <magento-home> is the installation directory for magento which is typically a directory within the web browser root directory, normally /var/www/html on a linux server.

Once the contents of the Contgit repo have been copied into the Magento directory structure this module must be installed into mageneto using the standard bin/magento setup:upgrade command.

It is also a pre-requisite to have the Contenful PHP module available as part of the Magento install. This can most easily be achieved using composer from within the <magento-home> directory wi=hich will place the Conteful library within the vendor directoty tree within Magento.

Using

Once installed and running the Contentful access token and space id are required parameters that are entered using the RomaTech menu option from within the Stores configuration settings.

Custom widgets can then be placed into the Magento web environment most easily using widgets embedded into the content section of various pages. A widget will require as a minimum the Contentful Entry id that will serve the data necessary to render the widget contents. Other parameters may also be required as either mandatory or optional parameters, depending on the widget design.

New Widgets

New widgets can be created to deliver new Magento frontend functionality or to display Contentful data in new ways.

Widgets require changes to a minimum of four files:

  1. A widget php code file.
  2. A widget layout phtml file
  3. Widget styling using less
  4. Widget configuration

Widget Code File

Widget code is created within the Block directory of the module with the php file name corresponding to the name of the widget to be created. This php file extends the Magento Block interface and within the constructor uses dependency injection to include the Contentful CMS data.

Contentful data can be easily retrieved within the Widget code using a standard call, for example:

$this->_contentful->getText('title')

This call will pull the named entry element, in the case title, from either the Magento cache or directly from Contentful in the format required. Text, RichText, Media, Number, Data and Time, Location and Boolean Contentful model types being supported.

Widget configuration parameters can also be retrieved using a standard call, for example:

$this->getData('link_to');

This call will pull the named parameter, in that case link_to, from the widget configuration. The name of the configuration parameter is the same that is defined in the Widget Configuration below.

Widget Layout File

Widget layout is defined using phtml within the view->frontend->templates->widget directory of the module with the phtml file name corresponding to the name of the widget.

This is a standard phtml file that is essentially html that pulls data from the widget code block using standard calls, for example:

<?php echo $block->getImageAlt(); ?>

Widget Styling

Widget styling uses the module less file located at view->frontend->web->css->source->module.less.

This file is a standard less file that may be used to style classes defined in the widget layout file.

Widget Configuration

Widget configuration parameters are defined in the etc->widget.xml file. These parameters will then be available for completion within the Magento backend when the widget is added to the environment.