Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding depth field to views #40

Open
wants to merge 2 commits into
base: 8.x-1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions bricks.module
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use Drupal\Core\Render\Element;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Component\Utility\Html;

/* BRICKS STORAGE */

Expand Down Expand Up @@ -39,6 +40,20 @@ function _bricks_field_schema_alter(&$schema) {
];
}


/**
* Implements hook_views_data_alter().
*/

function bricks_field_views_data_alter(array &$data, \Drupal\field\FieldStorageConfigInterface $field_storage) {
if($field_storage->getType() == 'bricks') {
$field_name = $field_storage->getName();
foreach($data as &$item) {
$item[$field_name . '_depth']['field']['id'] = 'bricksdepth';
}
}
}

/* BRICKS FORMATTING */

/**
Expand Down Expand Up @@ -78,6 +93,9 @@ function _bricks_nest_items($element, $items) {
$items[$delta]['#label'] = $element['#items'][$delta]->entity->label();
$items[$delta]['#delta'] = $delta;
$items[$delta]['#parent_delta'] = $parents[0];
$items[$delta]['#attributes']['class'][] = 'brick';
$items[$delta]['#attributes']['class'][] = 'brick--type--' . Html::cleanCssIdentifier($element['#items'][$delta]->entity->bundle());
$items[$delta]['#attributes']['class'][] = 'brick--id--' . $element['#items'][$delta]->entity->id();

$items[$delta]['childs'] = [];
if (!empty($element['#items'][$delta]->options['view_mode'])) {
Expand Down
100 changes: 100 additions & 0 deletions src/Plugin/views/field/BricksDepth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace Drupal\bricks\Plugin\views\field;

use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;

/**
* @ingroup views_field_handlers
*
* @ViewsField("bricksdepth")
*/
class BricksDepth extends FieldPluginBase {

/**
* {@inheritdoc}
*/
protected function defineOptions() {
$options = parent::defineOptions();
$options['style'] = ['default' => ''];
$options['prefix'] = ['default' => ''];
$options['suffix'] = ['default' => ''];
return $options;
}

/**
* {@inheritdoc}
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$form['style'] = [
'#type' => 'select',
'#title' => $this->t('Style'),
'#options' => [
'' => $this->t('Raw value'),
'space' => $this->t('Spaces'),
'line' => $this->t('Lines'),
'tree' => $this->t('Tree'),
],
'#default_value' => $this->options['style'],
];
$form['prefix'] = [
'#type' => 'textfield',
'#title' => $this->t('Prefix'),
'#default_value' => $this->options['prefix'],
'#description' => $this->t('Text to put before the number, such as currency symbol.'),
];
$form['suffix'] = [
'#type' => 'textfield',
'#title' => $this->t('Suffix'),
'#default_value' => $this->options['suffix'],
'#description' => $this->t('Text to put after the number, such as currency symbol.'),
];
parent::buildOptionsForm($form, $form_state);
}

/**
* {@inheritdoc}
*/
public function render(ResultRow $values) {
$raw_value = $this->getValue($values);

switch($this->options['style']) {
case 'space':
$indentation = [
'#theme' => 'indentation',
'#size' => $raw_value,
];
$value =\Drupal::service('renderer')->render($indentation);
break;
case 'tree':
if($raw_value > 0)
$value = '&nbsp;&nbsp;' . str_repeat('&nbsp;', ($raw_value - 1) * 4) . '└';
else
$value = '';
break;
case 'line':
$value = '└' . str_repeat('─', $raw_value);
break;
default:
$value = $raw_value;
break;
}

return [
'#markup' =>
$this->sanitizeValue($this->options['prefix'], 'xss')
. $value
. $this->sanitizeValue($this->options['suffix'], 'xss'),
];
}

// /**
// * {@inheritdoc}
// */
// public function clickSort($order) {
// $this->query->addOrderBy('node_field_data', 'created', $order);
// }
//
}