pattern_library-8.x-2.x-dev/src/Plugin/Pattern.php
src/Plugin/Pattern.php
<?php
namespace Drupal\pattern_library\Plugin;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Plugin\PluginBase;
use Drupal\pattern_library\Exception\InvalidPatternException;
/**
* Define the library pattern plugin.
*/
class Pattern extends PluginBase implements PatternInterface {
/**
* {@inheritDoc}
*/
public function getLabel() {
return $this->getPluginDefinition()['label'];
}
/**
* {@inheritDoc}
*/
public function getSource() {
return $this->typedDataDefinition()
->get('source')
->getValue();
}
/**
* {@inheritDoc}
*/
public function getStyles() {
return $this->typedDataDefinition()
->get('styles')
->getValue();
}
/**
* {@inheritDoc}
*/
public function getIcon() {
return $this->typedDataDefinition()
->get('icon')
->getValue();
}
/**
* {@inheritDoc}
*/
public function getIconMap() {
return $this->typedDataDefinition()
->get('icon_map')
->getValue();
}
/**
* {@inheritDoc}
*/
public function getRegions() {
return $this->typedDataDefinition()
->get('regions')
->getValue();
}
/**
* Has pattern library region.
*
* @param $name
* The machine name of the pattern region.
*
* @return bool
* Return TRUE if pattern region exist; otherwise FAlSE.
*
* @throws \Drupal\Core\TypedData\Exception\MissingDataException
* @throws \Drupal\pattern_library\Exception\InvalidPatternException
*/
public function hasRegion($name) {
return isset($this->getRegions()[$name]);
}
/**
* {@inheritDoc}
*/
public function getLayoutRegions() {
$regions = [];
$regions_keys = ['label'];
foreach ($this->getRegions() as $name => $region) {
$regions[$name] = array_intersect_key(
$region, array_flip($regions_keys)
);
}
return $regions;
}
public function getModifiers() {
return $this->typedDataDefinition()->get('modifiers')->getValue();
}
/**
* {@inheritDoc}
*/
public function getRegionModifier($region) {
return NestedArray::getValue(
$this->getRegions(), [$region, 'modifier']
);
}
/**
* {@inheritDoc}
*/
public function libraryKey() {
return "{$this->getProvider()}/{$this->getPluginId()}";
}
/**
* {@inheritDoc}
*/
public function getLibraries() {
return $this->typedDataDefinition()
->get('libraries')
->getValue();
}
/**
* {@inheritDoc}
*/
public function hasLibraries() {
return !empty($this->getLibraries());
}
/**
* {@inheritDoc}
*/
public function getProvider() {
return $this->typedDataDefinition()
->get('provider')
->getValue();
}
/**
* {@inheritDoc}
*/
public function validate() {
return empty($this->typedDataDefinition()->validate());
}
/**
* Pattern library typed data definition.
*
* @return \Drupal\pattern_library\Plugin\DataType\PatternLibrary
* The pattern library typed data definition.
*
* @throws \Drupal\pattern_library\Exception\InvalidPatternException
*/
protected function typedDataDefinition() {
if (!isset($this->configuration['typed_data'])) {
throw new InvalidPatternException(
'Unable to retrieve the pattern library typed data definition.'
);
}
return $this->configuration['typed_data'];
}
}
