julienmonnerie/kirby/src/Cms/Layout.php

106 lines
1.9 KiB
PHP
Raw Normal View History

2022-06-17 17:51:59 +02:00
<?php
namespace Kirby\Cms;
2025-04-21 18:57:21 +02:00
use Kirby\Content\Content;
2022-06-17 17:51:59 +02:00
/**
* Represents a single Layout with
* multiple columns
* @since 3.5.0
*
* @package Kirby Cms
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://getkirby.com/license
*/
class Layout extends Item
{
2022-08-31 15:02:43 +02:00
use HasMethods;
2022-06-17 17:51:59 +02:00
2022-12-19 14:56:05 +01:00
public const ITEMS_CLASS = Layouts::class;
2022-06-17 17:51:59 +02:00
2025-04-21 18:57:21 +02:00
protected Content $attrs;
protected LayoutColumns $columns;
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
/**
* Proxy for attrs
*/
2025-04-21 18:57:21 +02:00
public function __call(string $method, array $args = []): mixed
2022-08-31 15:02:43 +02:00
{
// layout methods
if ($this->hasMethod($method) === true) {
return $this->callMethod($method, $args);
}
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
return $this->attrs()->get($method);
}
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
/**
* Creates a new Layout object
*/
public function __construct(array $params = [])
{
parent::__construct($params);
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
$this->columns = LayoutColumns::factory($params['columns'] ?? [], [
2023-04-14 16:34:06 +02:00
'field' => $this->field,
2022-08-31 15:02:43 +02:00
'parent' => $this->parent
]);
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
// create the attrs object
$this->attrs = new Content($params['attrs'] ?? [], $this->parent);
}
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
/**
* Returns the attrs object
*/
2025-04-21 18:57:21 +02:00
public function attrs(): Content
2022-08-31 15:02:43 +02:00
{
return $this->attrs;
}
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
/**
* Returns the columns in this layout
*/
2025-04-21 18:57:21 +02:00
public function columns(): LayoutColumns
2022-08-31 15:02:43 +02:00
{
return $this->columns;
}
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
/**
* Checks if the layout is empty
* @since 3.5.2
*/
public function isEmpty(): bool
{
return $this
->columns()
2025-04-21 18:57:21 +02:00
->filter('isEmpty', false)
2022-08-31 15:02:43 +02:00
->count() === 0;
}
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
/**
* Checks if the layout is not empty
* @since 3.5.2
*/
public function isNotEmpty(): bool
{
return $this->isEmpty() === false;
}
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
/**
* The result is being sent to the editor
* via the API in the panel
*/
public function toArray(): array
{
return [
'attrs' => $this->attrs()->toArray(),
'columns' => $this->columns()->toArray(),
'id' => $this->id(),
];
}
2022-06-17 17:51:59 +02:00
}