View source
<?php
declare (strict_types=1);
namespace Drupal\views_ef_fieldset;
use RecursiveIteratorIterator;
class ViewsEFFieldsetData {
private $data;
private $elements;
private $form;
public function __construct(array $data, array &$form = []) {
$this->data = $data;
$this->elements = $data;
$this->form =& $form;
}
public function buildFlat() {
$data = [];
$recursive_iter_iter = new RecursiveIteratorIterator(new ArrayDataItemIterator($this
->buildTreeData()), RecursiveIteratorIterator::SELF_FIRST);
foreach ($recursive_iter_iter as $item) {
$item['item']['depth'] = $recursive_iter_iter
->getDepth();
$data[] = $item;
}
return $data;
}
public function buildTreeData() {
return $this
->parseTree($this->elements);
}
public function treetofapi() {
$elements = [];
$this
->recursivetreetofapi($this
->buildTreeData(), $this->form, $elements);
return $elements;
}
private function parseTree(array &$elements, $rootParentID = '', $depth = -1) {
$branch = [];
$depth++;
foreach ($elements as $key => $element) {
$element['depth'] = $depth;
if ($element['pid'] !== $rootParentID) {
continue;
}
$branch[] = [
'item' => $element,
'children' => $this
->parseTree($elements, $element['id'], $depth),
];
}
usort($branch, [
$this,
'sortByWeight',
]);
return empty($branch) ? [] : $branch;
}
private function recursivetreetofapi(array $data, array &$form, array &$element = []) {
foreach ($data as $key => $item) {
if ($item['item']['type'] === 'filter') {
$form_info = isset($form['#info']['filter-' . $item['item']['id']]) ? $form['#info']['filter-' . $item['item']['id']] : NULL;
$field_name = $form_info['value'] ?: $item['item']['id'];
if (isset($form[$field_name]) && is_array($form[$field_name])) {
$element[$field_name] = $form[$field_name] + [
'#weight' => $item['item']['weight'],
'#title' => $form_info['label'] ?: '',
'#description' => $form_info['description'] ?: '',
];
unset($form['#info']['filter-' . $item['item']['id']]);
unset($form[$field_name]);
}
}
if ($item['item']['type'] === 'sort') {
$field_name = $item['item']['id'];
if (isset($form[$field_name]) && is_array($form[$field_name])) {
$element[$field_name] = $form[$field_name];
$element[$field_name]['#weight'] = $item['item']['weight'];
unset($form[$field_name]);
}
}
if ($item['item']['type'] === 'buttons') {
$field_name = $item['item']['id'];
if (isset($form['actions'][$field_name]) && is_array($form['actions'][$field_name])) {
$button = $form['actions'][$field_name];
$button['#weight'] = $item['item']['weight'];
$button['#access'] = TRUE;
$element[$field_name] = $button;
$form['actions'][$field_name]['#attributes']['style'][] = 'display:none;';
}
}
if (!empty($item['children']) && $item['item']['type'] === 'container') {
$element['container-' . $item['item']['id']] = [
'#type' => $item['item']['container_type'],
'#title' => t('@title', [
'@title' => $item['item']['title'],
]),
'#group' => 'container-' . $item['item']['pid'],
'#description' => t('@description', [
'@description' => $item['item']['description'],
]),
'#open' => (bool) $item['item']['open'],
'#attributes' => [
'class' => [
'views-ef-fieldset-container',
'views-ef-fieldset-' . $item['item']['id'],
],
],
'#weight' => $item['item']['weight'],
];
$element['container-' . $item['item']['id']]['children'] = [];
$this
->recursivetreetofapi($item['children'], $form, $element['container-' . $item['item']['id']]['children']);
}
}
}
private function sortByWeight(array $a, array $b) {
if ($a['item']['weight'] === $b['item']['weight']) {
return 0;
}
return $a['item']['weight'] < $b['item']['weight'] ? -1 : 1;
}
}