*/ protected $baseAttributes = [ 'class' => ['item-list', 'default-layout'], 'data-base-target' => '_next', 'data-pdfexport-page-breaks-at' => '.list-item' ]; /** @var ResultSet|iterable */ protected $data; protected $tag = 'ul'; /** @var ?string Message to show if the list is empty */ protected $emptyStateMessage; /** * Create a new item list * * @param ResultSet|iterable $data Data source of the list */ public function __construct($data) { if (! is_iterable($data)) { throw new InvalidArgumentException('Data must be an array or an instance of Traversable'); } $this->data = $data; $this->addAttributes($this->baseAttributes); $this->init(); } abstract protected function getItemClass(): string; /** * Initialize the item list * * If you want to adjust the item list after construction, override this method. */ protected function init(): void { } /** * Create a list item for the given data * * @param object $data * * @return BaseListItem|BaseTableRowItem */ protected function createListItem(object $data) { $className = $this->getItemClass(); return new $className($data, $this); } /** * Get message to show if the list is empty * * @return string */ public function getEmptyStateMessage(): string { if ($this->emptyStateMessage === null) { return t('No items found.'); } return $this->emptyStateMessage; } /** * Set message to show if the list is empty * * @param string $message * * @return $this */ public function setEmptyStateMessage(string $message): self { $this->emptyStateMessage = $message; return $this; } protected function assemble(): void { foreach ($this->data as $data) { $item = $this->createListItem($data); $this->emit(self::BEFORE_ITEM_ADD, [$item, $data]); $this->addHtml($item); $this->emit(self::ON_ITEM_ADD, [$item, $data]); } if ($this->isEmpty()) { $this->setTag('div'); $this->addHtml(new EmptyStateBar($this->getEmptyStateMessage())); } } }