M2: Invalidating Caches

When saving objects in your custom module it may be beneficial to indicate that a cache type should be cleared.  This can be achieved using the TypeListInterface from the Framework’s Cache Module. Furthermore with this interface you can also retrieve the cache type labels, a list of other caches that have been invalidated, or clean a cache type by code.

<?php

namespace Vendor\Module\Folder;

use Magento\Framework\App\Cache\TypeListInterface;

/**
 * Class Example
 *
 * @package Vendor\Module\Folder
 */
class Example {

   /**
   * @var TypeListInterface
   */
   protected $_cacheTypesList;

   /**
    * Example constructor.
    *
    * @param TypeListInterface $_cacheTypesList
    */
   public function __construct( TypeListInterface $_cacheTypesList )
   {
      $this->_cacheTypesList = $_cacheTypesList;
   }

   /**
    * Mark specific cache type(s) as invalidated
    *
    * @param $type
    */
   public function invalidateCache( $type )
   {
      $this->_cacheTypesList->invalidate( $type );
   }

   /**
    * Get information about all declared cache types
    *
    * @return array
    */
   public function getCacheTypes()
   {
      /* Example structure for cache type array
       $_types = [
         'CACHE TYPE CODE' =>
          (object)[ //Magento\Framework\DataObject
                    'id' =>(int)'',
                    'cache_type'=>(string) '',
                    'description'=>(string)'',
                    'tags'=>(string)'',
                     'status'=>(int)'',
             ]
      ];*/
      return $this->_cacheTypesList->getTypes();
   }

}

M2: Loading model collections the Magento 2.x way

It is now be the responsibility of the repository to load and persist models from the database rather than using the model to load and save directly. This can be confirmed by the load and save methods being marked as @deprecated in \Magento\Framework\Model\AbstractModel.


use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\Search\FilterGroupBuilder;
//your model repo, etc...
.......

$_searchCriteria = $this->_searchCriteriaBuilder->create();

$filterA = $this->_filterBuilder->setField( $fieldName ) //Filter by model field name
->setValue( $tagValue ) //Filter value
->setConditionType( 'eq' ) //Comparison operator
->create();

$filterGroupA = $this->_filterGroupBuilder->setFilters( [ $filterA ] )->create();

//Filters in the same groups work like OR
//Multiple filter groups work like AND
//Which is why the filter and the group are noted with as [$a]

$_searchCriteria->setFilterGroups( [ $filterGroupA ] );

/** @var Model[] $_results */
$_results = $this->_modelRepository->getList( $_searchCriteria )->getItems();

return $_results;

M2: Add a CMS block to your layout

Add a CMS block to your layout XML:

<referenceContainer name="containerName">
    <block class="Magento\Cms\Block\Block" name="block_name_in_layout" as="block.name.in.layout">
        <arguments>
            <argument name="block_id" xsi:type="string">block_identifier</argument>
        </arguments>
    </block>
</referenceContainer>