Skip to content

Instantly share code, notes, and snippets.

@Seorusus
Created October 8, 2022 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Seorusus/805219e690395bee8d896796d8fc602d to your computer and use it in GitHub Desktop.
Save Seorusus/805219e690395bee8d896796d8fc602d to your computer and use it in GitHub Desktop.
<?php
namespace Drupal\basf_twig\BridgeElements\Atom;
use Drupal\basf_twig\BridgeElements\ElementInterface;
use Drupal\paragraphs\ParagraphInterface;
class ListGroup implements ElementInterface {
/**
* Feature list Icon.
*
* @var \Drupal\basf_twig\BridgeElements\Atom\Icon|null
*/
private ?Icon $icon = NULL;
/**
* Items of List Group.
*
* @var array
*/
private array $items = [];
/**
* List Group background.
*
* @var bool
*/
private bool $background = FALSE;
/**
* Constructs the Atom "List Group" object.
*/
private function __construct(
array $options = NULL,
) {
// Copy all options to the object.
foreach ($options as $option => $optionValue) {
if (property_exists($this, $option)) {
$this->$option = $optionValue;
}
}
}
/**
* Creates List Group object.
*
* @param \Drupal\paragraphs\ParagraphInterface $paragraph
* List Group Paragraph.
*
* @return static
* List Group object.
* @throws \Exception
*/
public static function create(ParagraphInterface $paragraph): ListGroup {
$options = [];
if (!$paragraph->field_list_items->isEmpty()) {
$options['items'] = ListItem::createFromField($paragraph->field_list_items);
}
return new self($options);
}
/**
* {@inheritdoc}
*/
public function toArray(): array {
return array_filter([
'icon' => $this->icon,
'items' => $this->getListItem(),
'background' => $this->background,
]);
}
/**
* Set or delete icon for the List Group.
*
* @param \Drupal\basf_twig\BridgeElements\Atom\Icon|null $icon
* List Group icon.
*
* @return ListGroup
* List Group object.
*/
public function setIcon(?Icon $icon): ListGroup {
$this->icon = $icon;
return $this;
}
/**
* Provides list Items for the entity.
*
* @return array
* list Items or empty array if no list Items here.
*/
private function getListItem(): array {
return $this->items;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment