I am new to Drupal 8. I make custom view field (programmatically) and pass session variable to it.Session variable contain group id. The problem is that when the view is empty i mean no content in view then the group id not shown.When the view is not empty the group id shown.check out below code.
logged_in.views.inc
<?php
/**
* @file
* Contains logged_in\logged_in.views.inc..
* Provide a custom views field data that isn't tied to any other module. */
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\Core\Render\Markup;
use Drupal\field\FieldConfigInterface;
use Drupal\field\FieldStorageConfigInterface;
use Drupal\system\ActionConfigEntityInterface;
/**
* Implements hook_views_data().
*/
function logged_in_views_data() {
$data['views']['table']['group'] = t('Custom Global');
$data['views']['table']['join'] = [
// #global is a special flag which allows a table to appear all the time.
'#global' => [],
];
$data['views']['logged_in'] = [
'title' => t('Logged in'),
'help' => t('Checks if a user is logged in.'),
'field' => [
'id' => 'logged_in',
],
];
return $data;
}
<?php
namespace Drupal\logged_in\Plugin\views\field;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
/**
* A handler to provide a field that is completely custom by the administrator.
*
* @ingroup views_field_handlers
*
* @ViewsField("logged_in")
*/
class LoggedIn extends FieldPluginBase {
/**
* {@inheritdoc}
*/
public function usesGroupBy() {
return FALSE;
}
/**
* {@inheritdoc}
*/
public function query() {
// Do nothing -- to override the parent query.
}
/**
* {@inheritdoc}
*/
protected function defineOptions() {
$options = parent::defineOptions();
$options['hide_alter_empty'] = ['default' => FALSE];
return $options;
}
/**
* {@inheritdoc}
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function render(ResultRow $values) {
// Return a random text, here you can include your custom logic.
// Include any namespace required to call the method required to generate
// the desired output.
// $logged_in = \Drupal::currentUser()->isAuthenticated();
// $logged_in = \Drupal::currentUser()->id();
$logged_in = (int)$_SESSION['groupbysession']['group_id'];
return $logged_in;
}
<div class="all-content-heading"><span><h3>All Session</h3></span></div>
<div class="all-content-button"><span><a class="pull-right " href="group/{{ logged_in | striptags | spaceless }}/content/create/group_node%3Asessions">Add Session</a></span></div>
Display even if view has no result
box?NO RESULTS BEHAVIOR
add text area here.{{ logged_in }}
token.