<?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_projets_reference")
 */
class ViewsComputedProjetsReference extends FieldPluginBase {

  /**
   * {@inheritdoc}
   */
  public function render(ResultRow $values) {
    $relationship_entities = $values->_relationship_entities;
    $projets = 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') {
      $projets = $node->get('computed_projets_references')->getvalue();
    }

    return count($projets) ? count($projets) . ' villes' : null;
  }

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