View source
<?php
namespace Drupal\views_fieldsets;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Render\Markup;
use Drupal\views\ResultRow;
use Drupal\views_fieldsets\Plugin\views\field\Fieldset;
class RowFieldset {
public $row;
public $properties = [];
public $children = [];
public function __construct($field, ResultRow $row) {
$this->row = $row;
$this->properties = get_object_vars($field);
}
public function __isset($name) {
return TRUE;
}
public function __get($name) {
$method_name = 'get' . Unicode::ucwords($name);
if (is_callable($method = [
$this,
$method_name,
])) {
return call_user_func($method);
}
if (!empty($name) && !empty($this->properties[$name])) {
return $this->properties[$name];
}
return FALSE;
}
public function getContent() {
return $this
->render();
}
public function getWrapperelement() {
return '';
}
public function getElementtype() {
return '';
}
public function render() {
$element = [
'#theme' => 'views_fieldsets_' . $this
->getWrapperType(),
'#fields' => $this->children,
'#legend' => Markup::create($this
->getLegend()),
'#collapsible' => (bool) $this->handler->options['collapsible'],
'#attributes' => [
'class' => $this
->getClasses(),
],
];
if ($this->handler->options['collapsed'] && $this
->getWrapperType() != 'div') {
$element['#attributes']['class'][] = 'collapsed';
}
return render($element);
}
protected function getWrapperType() {
$allowed = Fieldset::getWrapperTypes();
$wrapper = $this->handler->options['wrapper'];
if (isset($allowed[$wrapper])) {
return $wrapper;
}
reset($allowed);
return key($allowed);
}
protected function getLegend() {
return $this
->tokenize($this->handler->options['legend']);
}
protected function getClasses() {
$classes = explode(' ', $this->handler->options['classes']);
$classes = array_map(function ($class) {
return Html::getClass($this
->tokenize($class));
}, $classes);
return $classes;
}
protected function tokenize($string) {
return $this->handler
->tokenizeValue($string, $this->row->index);
}
public function addChild(array $fields, $field_name) {
$this->children[$field_name] = $fields[$field_name];
}
}