Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
ThemeList Class Reference
Inheritance diagram for ThemeList:
Collection ListInterface ArrayInterface CollectionDataSourceInterface OptionSourceInterface ArgumentInterface Collection Collection

Public Member Functions

 __construct (\Magento\Framework\Data\Collection\EntityFactory $entityFactory, \Magento\Framework\Config\ThemeFactory $themeConfigFactory, ThemePackageList $themePackageList, ReadFactory $dirReadFactory)
 
 addConstraint ($type, $value)
 
 resetConstraints ()
 
 loadData ($printQuery=false, $logQuery=false)
 
 toOptionArray ($addEmptyField=false)
 
 hasTheme (ThemeInterface $theme)
 
 getThemeByFullPath ($fullPath)
 
- Public Member Functions inherited from Collection
 __construct (EntityFactoryInterface $entityFactory)
 
 addFilter ($field, $value, $type='and')
 
 addFieldToFilter ($field, $condition)
 
 getFilter ($field)
 
 isLoaded ()
 
 getCurPage ($displacement=0)
 
 getLastPageNumber ()
 
 getPageSize ()
 
 getSize ()
 
 getFirstItem ()
 
 getLastItem ()
 
 getItems ()
 
 getColumnValues ($colName)
 
 getItemsByColumnValue ($column, $value)
 
 getItemByColumnValue ($column, $value)
 
 addItem (\Magento\Framework\DataObject $item)
 
 getAllIds ()
 
 removeItemByKey ($key)
 
 removeAllItems ()
 
 clear ()
 
 walk ($callback, array $args=[])
 
 each ($objMethod, $args=[])
 
 setDataToAll ($key, $value=null)
 
 setCurPage ($page)
 
 setPageSize ($size)
 
 setOrder ($field, $direction=self::SORT_ORDER_DESC)
 
 setItemObjectClass ($className)
 
 getNewEmptyItem ()
 
 distinct ($flag)
 
 loadData ($printQuery=false, $logQuery=false)
 
 load ($printQuery=false, $logQuery=false)
 
 loadWithFilter ($printQuery=false, $logQuery=false)
 
 toXml ()
 
 toArray ($arrRequiredFields=[])
 
 toOptionArray ()
 
 toOptionHash ()
 
 getItemById ($idValue)
 
 getIterator ()
 
 count ()
 
 getFlag ($flag)
 
 setFlag ($flag, $value=null)
 
 hasFlag ($flag)
 
 __sleep ()
 
 __wakeup ()
 

Data Fields

const CONSTRAINT_AREA = 'area'
 
const CONSTRAINT_VENDOR = 'vendor'
 
const CONSTRAINT_THEME_NAME = 'theme_name'
 
- Data Fields inherited from Collection
const SORT_ORDER_ASC = 'ASC'
 
const SORT_ORDER_DESC = 'DESC'
 

Protected Member Functions

 _loadFromFilesystem (array $themes)
 
 _preparePathData ($themePackage)
 
 _prepareConfigurationData ($themePackage)
 
 _clearFilters ()
 
 _getConfigModel ($themePackage)
 
 _getItemId (\Magento\Framework\DataObject $item)
 
- Protected Member Functions inherited from Collection
 _setIsLoaded ($flag=true)
 
 _addItem ($item)
 
 _getItemId (\Magento\Framework\DataObject $item)
 
 _renderFilters ()
 
 _renderOrders ()
 
 _renderLimit ()
 
 _toOptionArray ($valueField='id', $labelField='name', $additional=[])
 
 _toOptionHash ($valueField='id', $labelField='name')
 

Protected Attributes

 $_itemObjectClass = ThemeInterface::class
 
 $themeConfigFactory
 
- Protected Attributes inherited from Collection
 $_items = []
 
 $_itemObjectClass = \Magento\Framework\DataObject::class
 
 $_orders = []
 
 $_filters = []
 
 $_isFiltersRendered = false
 
 $_curPage = 1
 
 $_pageSize = false
 
 $_totalRecords
 
 $_isCollectionLoaded
 
 $_flags = []
 
 $_entityFactory
 

Detailed Description

Theme filesystem collection

Definition at line 14 of file ThemeList.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\Data\Collection\EntityFactory  $entityFactory,
\Magento\Framework\Config\ThemeFactory  $themeConfigFactory,
ThemePackageList  $themePackageList,
ReadFactory  $dirReadFactory 
)

Constructor

Parameters
\Magento\Framework\Data\Collection\EntityFactory$entityFactory
\Magento\Framework\Config\ThemeFactory$themeConfigFactory
ThemePackageList$themePackageList
ReadFactory$dirReadFactory

Definition at line 80 of file ThemeList.php.

85  {
86  parent::__construct($entityFactory);
87  $this->themeConfigFactory = $themeConfigFactory;
88  $this->themePackageList = $themePackageList;
89  $this->dirReadFactory = $dirReadFactory;
90  }
$themePackageList
Definition: bootstrap.php:88

Member Function Documentation

◆ _clearFilters()

_clearFilters ( )
protected

Clear all added filters

Returns
$this

Definition at line 287 of file ThemeList.php.

288  {
289  $this->_filters = [];
290  return $this;
291  }

◆ _getConfigModel()

_getConfigModel (   $themePackage)
protected

Return configuration model for the theme

Parameters
ThemePackage$themePackage
Returns
\Magento\Framework\Config\Theme

Definition at line 299 of file ThemeList.php.

300  {
301  $themeDir = $this->dirReadFactory->create($themePackage->getPath());
302  if ($themeDir->isExist('theme.xml')) {
303  $configContent = $themeDir->readFile('theme.xml');
304  } else {
305  $configContent = '';
306  }
307  return $this->themeConfigFactory->create(['configContent' => $configContent]);
308  }

◆ _getItemId()

_getItemId ( \Magento\Framework\DataObject  $item)
protected

Retrieve item id

Parameters
\Magento\Framework\DataObject$item
Returns
string

Definition at line 316 of file ThemeList.php.

317  {
318  return $item->getFullPath();
319  }

◆ _loadFromFilesystem()

_loadFromFilesystem ( array  $themes)
protected

Load themes collection from file system

Parameters
ThemePackage[]$themes
Returns
$this

Definition at line 197 of file ThemeList.php.

198  {
199  foreach ($themes as $themePackage) {
200  $theme = $this->getNewEmptyItem()->addData($this->_prepareConfigurationData($themePackage));
201  $this->addItem($theme);
202  }
203  $this->_setIsLoaded();
204 
205  return $this;
206  }
$theme
addItem(\Magento\Framework\DataObject $item)
Definition: Collection.php:399

◆ _prepareConfigurationData()

_prepareConfigurationData (   $themePackage)
protected

Return default configuration data

Parameters
ThemePackage$themePackage
Returns
array

Definition at line 230 of file ThemeList.php.

231  {
232  $themeConfig = $this->_getConfigModel($themePackage);
233  $pathData = $this->_preparePathData($themePackage);
234  $media = $themeConfig->getMedia();
235 
236  $parentPathPieces = $themeConfig->getParentTheme();
237  if (is_array($parentPathPieces) && count($parentPathPieces) == 1) {
238  $pathPieces = $pathData['theme_path_pieces'];
239  array_pop($pathPieces);
240  $parentPathPieces = array_merge($pathPieces, $parentPathPieces);
241  }
242 
243  $themePath = implode(ThemeInterface::PATH_SEPARATOR, $pathData['theme_path_pieces']);
244  $themeCode = implode(ThemeInterface::CODE_SEPARATOR, $pathData['theme_path_pieces']);
245  $parentPath = $parentPathPieces ? implode(ThemeInterface::PATH_SEPARATOR, $parentPathPieces) : null;
246 
247  return [
248  'parent_id' => null,
250  'area' => $themePackage->getArea(),
251  'theme_path' => $themePath,
252  'code' => $themeCode,
253  'theme_title' => $themeConfig->getThemeTitle(),
254  'preview_image' => $media['preview_image'] ? $media['preview_image'] : null,
255  'parent_theme_path' => $parentPath
256  ];
257  }

◆ _preparePathData()

_preparePathData (   $themePackage)
protected

Return default path related data

Parameters
ThemePackage$themePackage
Returns
array

Definition at line 214 of file ThemeList.php.

215  {
216  return [
217  'theme_path_pieces' => [
218  $themePackage->getVendor(),
219  $themePackage->getName(),
220  ]
221  ];
222  }

◆ addConstraint()

addConstraint (   $type,
  $value 
)

Add constraint for the collection loading

See CONSTRAINT_* constants for supported types

Parameters
string$type
string$value
Returns
void

Definition at line 101 of file ThemeList.php.

102  {
103  if (!isset($this->constraints[$type])) {
104  throw new \UnexpectedValueException("Constraint '$type' is not supported");
105  }
106  if ($this->isLoaded()) {
107  $this->clear();
108  }
109  $this->constraints[$type][] = $value;
110  $this->constraints[$type] = array_unique($this->constraints[$type]);
111  }
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16

◆ getThemeByFullPath()

getThemeByFullPath (   $fullPath)

Get theme from file system by area and theme_path

Parameters
string$fullPath
Returns
ThemeInterface

Implements ListInterface.

Definition at line 351 of file ThemeList.php.

352  {
353  list($area, $themePath) = explode('/', $fullPath, 2);
354  $this->addConstraint(self::CONSTRAINT_AREA, $area);
355  $this->addFilter('theme_path', $themePath);
356 
357  return $this->getFirstItem();
358  }
addFilter($field, $value, $type='and')
Definition: Collection.php:118

◆ hasTheme()

hasTheme ( ThemeInterface  $theme)

Checks that a theme present in filesystem collection

Parameters
ThemeInterface$theme
Returns
bool

Definition at line 339 of file ThemeList.php.

340  {
341  $themeItems = $this->getItems();
342  return $theme->getThemePath() && isset($themeItems[$theme->getFullPath()]);
343  }
$theme

◆ loadData()

loadData (   $printQuery = false,
  $logQuery = false 
)

Fill collection with theme model loaded from filesystem

Parameters
bool$printQuery
bool$logQuery
Returns
$this @SuppressWarnings(PHPMD.UnusedFormalParameter)

Definition at line 145 of file ThemeList.php.

146  {
147  if ($this->isLoaded()) {
148  return $this;
149  }
150 
151  $themes = [];
152  foreach ($this->themePackageList->getThemes() as $themePackage) {
153  if ($this->isAcceptable(self::CONSTRAINT_AREA, $themePackage->getArea())
154  && $this->isAcceptable(self::CONSTRAINT_VENDOR, $themePackage->getVendor())
155  && $this->isAcceptable(self::CONSTRAINT_THEME_NAME, $themePackage->getName())
156  ) {
157  $themes[] = $themePackage;
158  }
159  }
160 
161  $this->_loadFromFilesystem($themes);
162  $this->resetConstraints();
163  $this->_updateRelations()
164  ->_renderFilters()
165  ->_clearFilters();
166 
167  return $this;
168  }

◆ resetConstraints()

resetConstraints ( )

Reset constraints for the collection loading

Returns
void

Definition at line 118 of file ThemeList.php.

119  {
120  foreach (array_keys($this->constraints) as $key) {
121  $this->constraints[$key] = [];
122  }
123  }

◆ toOptionArray()

toOptionArray (   $addEmptyField = false)

Return array for select field

Parameters
bool$addEmptyField
Returns
array

Definition at line 327 of file ThemeList.php.

328  {
329  $optionArray = $addEmptyField ? ['' => ''] : [];
330  return $optionArray + $this->_toOptionArray('theme_id', 'theme_title');
331  }
_toOptionArray($valueField='id', $labelField='name', $additional=[])
Definition: Collection.php:760

Field Documentation

◆ $_itemObjectClass

$_itemObjectClass = ThemeInterface::class
protected

Definition at line 40 of file ThemeList.php.

◆ $themeConfigFactory

Magento Framework Config ThemeFactory $themeConfigFactory
protected

Definition at line 45 of file ThemeList.php.

◆ CONSTRAINT_AREA

const CONSTRAINT_AREA = 'area'

Area constraint type

Definition at line 19 of file ThemeList.php.

◆ CONSTRAINT_THEME_NAME

const CONSTRAINT_THEME_NAME = 'theme_name'

Theme name constraint type

For example, "blank" part for theme "frontend/Magento/blank"

Definition at line 33 of file ThemeList.php.

◆ CONSTRAINT_VENDOR

const CONSTRAINT_VENDOR = 'vendor'

Vendor constraint type

For example, "Magento" part for theme "frontend/Magento/blank"

Definition at line 26 of file ThemeList.php.


The documentation for this class was generated from the following file: