class MiconIcon in Micon 2.x
Same name and namespace in other branches
- 8 src/MiconIcon.php \Drupal\micon\MiconIcon
Defines the Micon icon.
Hierarchy
- class \Drupal\micon\MiconIcon implements RenderableInterface, MiconIconInterface
Expanded class hierarchy of MiconIcon
File
- src/
MiconIcon.php, line 12
Namespace
Drupal\miconView source
class MiconIcon implements MiconIconInterface, RenderableInterface {
/**
* The Micon type. Either 'font' or 'image'.
*
* @var string
*/
protected $type;
/**
* The Micon icon data.
*
* @var array
*/
protected $data;
/**
* The Attribute object.
*
* @var \Drupal\Core\Template\Attribute
*/
protected $attributes;
/**
* Constructs a new MiconIcon.
*
* @param string $type
* The type of icon. Either 'font' or 'image'.
* @param array $data
* The icon data array provided from the Micon package info file.
* @param array $attributes
* The attributes to add to the group wrapper.
*/
public function __construct($type, array $data, array $attributes = []) {
$this->type = $type;
$this->data = $data;
$this
->setAttributes($attributes);
}
/**
* {@inheritdoc}
*/
public function getType() {
return $this->type;
}
/**
* {@inheritdoc}
*/
public function getPackageId() {
return $this->data['package_id'];
}
/**
* {@inheritdoc}
*/
public function getPackageLabel() {
return $this->data['package_label'];
}
/**
* {@inheritdoc}
*/
public function getPrefix() {
return $this->data['prefix'];
}
/**
* {@inheritdoc}
*/
public function getName() {
return $this->data['name'];
}
/**
* {@inheritdoc}
*/
public function getTags() {
return $this->data['tags'];
}
/**
* {@inheritdoc}
*/
public function getSelector() {
return $this
->getPrefix() . $this
->getName();
}
/**
* {@inheritdoc}
*/
public function getHex() {
return $this->type == 'font' ? '\\' . dechex($this->data['properties']['code']) : '';
}
/**
* {@inheritdoc}
*/
public function getWrappingElement() {
return $this->type == 'image' ? 'svg' : 'i';
}
/**
* {@inheritdoc}
*/
public function getChildren() {
$build = [];
if ($this->type == 'font') {
// Font glyphs cannot have more than one color by default. Using CSS,
// IcoMoon layers multiple glyphs on top of each other to implement
// multicolor glyphs. As a result, these glyphs take more than one
// character code and cannot have ligatures. To avoid multicolor glyphs,
// reimport your SVG after changing all its colors to the same color.
if (!empty($this->data['properties']['codes']) && count($this->data['properties']['codes'])) {
for ($i = 1; $i <= count($this->data['properties']['codes']); $i++) {
$build[]['#markup'] = '<span class="path' . $i . '"></span>';
}
}
}
if ($this->type == 'image') {
$build['#markup'] = Markup::create('<use xlink:href="' . $this->data['directory'] . '/symbol-defs.svg#' . $this
->getSelector() . '"></use>');
$build['#allowed_tags'] = [
'use',
'xlink',
];
}
return $build;
}
/**
* {@inheritdoc}
*/
public function addClass($classes) {
$this->attributes
->addClass($classes);
return $this;
}
/**
* {@inheritdoc}
*/
public function setAttributes(array $attributes) {
$this->attributes = new Attribute($attributes);
return $this;
}
/**
* {@inheritdoc}
*/
public function setAttribute($attribute, $value) {
$this->attributes
->setAttribute($attribute, $value);
return $this;
}
/**
* {@inheritdoc}
*/
public function toRenderable() {
return [
'#theme' => 'micon_icon',
'#icon' => $this,
'#attributes' => $this->attributes
->toArray(),
];
}
/**
* {@inheritdoc}
*/
public function toMarkup() {
$elements = $this
->toRenderable();
return \Drupal::service('renderer')
->render($elements);
}
/**
* {@inheritdoc}
*/
public function toJson() {
$elements = $this
->toRenderable();
$elements['#attributes']['data-tags'] = $this
->getTags();
$markup = \Drupal::service('renderer')
->render($elements);
return json_encode(trim(preg_replace('/<!--(.|\\s)*?-->/', '', $markup
->jsonSerialize())));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MiconIcon:: |
protected | property | The Attribute object. | |
MiconIcon:: |
protected | property | The Micon icon data. | |
MiconIcon:: |
protected | property | The Micon type. Either 'font' or 'image'. | |
MiconIcon:: |
public | function |
Adds classes or merges them on to array of existing CSS classes. Overrides MiconIconInterface:: |
|
MiconIcon:: |
public | function |
Get the content entered within the icon tags. Overrides MiconIconInterface:: |
|
MiconIcon:: |
public | function |
Get the HEX used within sudo-elements of CSS. Overrides MiconIconInterface:: |
|
MiconIcon:: |
public | function |
Get the unique name of the IcoMoon icon. Overrides MiconIconInterface:: |
|
MiconIcon:: |
public | function |
Get the id of the Micon package. Overrides MiconIconInterface:: |
|
MiconIcon:: |
public | function |
Get the label of the Micon package. Overrides MiconIconInterface:: |
|
MiconIcon:: |
public | function |
Get the unique prefix of the IcoMoon package. Overrides MiconIconInterface:: |
|
MiconIcon:: |
public | function |
Get the unique selector of the IcoMoon icon. Overrides MiconIconInterface:: |
|
MiconIcon:: |
public | function |
Get the tags of the IcoMoon icon. Overrides MiconIconInterface:: |
|
MiconIcon:: |
public | function |
Get the type of the IcoMoon package. Overrides MiconIconInterface:: |
|
MiconIcon:: |
public | function |
Get the wrapping element HTML element type. Overrides MiconIconInterface:: |
|
MiconIcon:: |
public | function |
Sets values for an attribute key. Overrides MiconIconInterface:: |
|
MiconIcon:: |
public | function |
Sets attributes. Overrides MiconIconInterface:: |
|
MiconIcon:: |
public | function |
Returns a trimmed, json encoded string of the rendered markup. Overrides MiconIconInterface:: |
|
MiconIcon:: |
public | function |
Returns a fully rendered Markup representation of the object. Overrides MiconIconInterface:: |
|
MiconIcon:: |
public | function |
Returns a render array representation of the object. Overrides MiconIconInterface:: |
|
MiconIcon:: |
public | function | Constructs a new MiconIcon. |