<?php

/**
 * Implements hook_ENTITY_TYPE_update() for Drutopia article content.
 */
function geo_citation_article_update(Drupal\Core\Entity\EntityInterface $entity)
{
  $article_type = $entity->field_article_type->getValue();
  // If it's not a GEO Original article, there's nothing for us to do.
  if (!$article_type == 6) {
    return;
  }
}

/**
 * Implements hook_entity_extra_field_info().
 */
function geo_citation_entity_extra_field_info() {
  $extra = [];
  $extra['node']['article']['display']['geo_citation'] = [
    'label' => t('GEO Citation'),
    'description' => t('Re-presents title, author, date and other information in a standard citeable format.'),
    'weight' => 90,
  ];
  return $extra;
}

/**
 * Implements hook_ENTITY_TYPE_view() for nodes.
 */
function geo_citation_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
  // This will only exist for the Drutopia article content type but don't think we need to check first.
  if ($display->getComponent('geo_citation')) {
    $build['geo_citation'] = [
      '#markup' => geo_citation_pseudo_field($entity),
//      '#theme' => 'geo_citation_field',
    ];
  }

}

/**
 * Implements hook_theme().
 *
 * We intentionally choose to do the citation in one big lump, because our
 * expectation for making this more flexible would be to plug into bibcite or
 * other citation API, rather than making it easy to re-theme this custom data.
 *
function geo_citation_theme($existing, $type, $theme, $path) {
  return [
    'geo_citation_field' => [
      'variables' => [
        'citation' => '',
      ]
    ]
  ]
}
 * ...and it turns out giving theming capability to our pseudofield is optional.
 */

/**
 * Assemble and return our citation as already-marked-up HTML.
 */
function geo_citation_pseudo_field($entity) {
  $markup = '';

  // This returns a numerically-indexed array of arrays with little in them
  // except the taxonomy term 'target_id' as a stringified number.
  $article_types = $entity->field_article_type->getValue();
  // This looks for the index matching 'GEO Original' (term ID 303), which will
  // often be zero when it's the only selected article type, so we must be sure
  // to test for a false identity and not accidentally reject zero.
  if (array_search('303', array_column($article_types, 'target_id')) === FALSE) {
    // If it's not a GEO Original article, we do not add a citation.
    return $markup;
  }

  $authors = '';
  foreach ($entity->field_authors as $i => $author) {
    // @TODO replace this with https://packagist.org/packages/agaric/oxford-comma
    $authors .= ($i > 0) ? ', ' : '';
    $authors .= isset($author->entity) ? $author->entity->title->value : '';
  }
  $date = $entity->field_publication_date->value;
  $year = substr($date, 0, 4);
  $title = $entity->title->value;
  $subtitle = $entity->field_subtitle->value;
  $url = $entity->toUrl()->setAbsolute()->toString();

  $markup .= '<div class="drum geo-citation">';

  $tooltip = t("When citing this article, please use this format.");
  $markup .= '<h5 class="title is-5"><abbr title="' . $tooltip . '">' . t("Citations") . '</abbr></h5>';

  $markup .= '<p>';
  $markup .= "$authors ($year).&#8198; $title";
  if ($subtitle) {
    $markup .= ":&#8198; $subtitle";
  }
  $markup .= ".&#8198; ";
  $markup .= t("Grassroots Economic Organizing (GEO).");
  $markup .= "&#8198; $url";
  $markup .= '</p>';

  $markup .= '</div>';

  return $markup;
}
