0

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;
  }

} enter image description here

<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>
6
  • In these settings. at the very top, did you check the Display even if view has no result box?
    – No Sssweat
    Commented Mar 11, 2020 at 8:20
  • Thanks for the reply. Yes the box is checked.But this box is for header. if view has no content also show the header.
    – Dev-Kaleem
    Commented Mar 11, 2020 at 8:34
  • In the main Views settings, below FOOTER, there is NO RESULTS BEHAVIOR add text area here.
    – No Sssweat
    Commented Mar 11, 2020 at 8:40
  • I was also tried this but the same problem.it also doesn't show me group id in path.
    – Dev-Kaleem
    Commented Mar 11, 2020 at 9:18
  • You'll need to pass the variable programmatically directly to the "no results behaviour" instead of using a {{ logged_in }} token.
    – No Sssweat
    Commented Mar 11, 2020 at 9:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.