<?php

namespace Drupal\popsu_programme\Plugin\views\field;

use Drupal\views\ResultRow;
use Drupal\views\Plugin\views\field\FieldPluginBase;

/**
 * A handler to provide proper displays for profile current company.
 *
 * @ingroup views_field_handlers
 *
 * @ViewsField("views_computed_themes_reference")
 */
class ViewsComputedThemesReference extends FieldPluginBase {

  /**
   * {@inheritdoc}
   */
  public function render(ResultRow $values) {
    $relationship_entities = $values->_relationship_entities;
    $themes = null;
    // First check the referenced entity.
    if (isset($relationship_entities['node'])) {
      $node = $relationship_entities['node'];
    }
    else {
      $node = $values->_entity;
    }

    $type = get_class($node);
    if ($type === 'Drupal\node\Entity\Node') {
      $themes = $node->get('computed_themes_references')->getvalue();
    }

    return count($themes) ? count($themes) . ' thèmes' : null;
  }

  /**
   * {@inheritdoc}
   */
  public function query() {
    // This function exists to override parent query function.
    // Do nothing.
  }
}