diff --git a/web/modules/custom/geo_citation/geo_citation.module b/web/modules/custom/geo_citation/geo_citation.module index cda6778..ec35b2d 100644 --- a/web/modules/custom/geo_citation/geo_citation.module +++ b/web/modules/custom/geo_citation/geo_citation.module @@ -26,8 +26,72 @@ function geo_citation_entity_extra_field_info() { } /** - * Implements hook_ENTITY_TYPE_view() for Drutopia articles. + * Implements hook_ENTITY_TYPE_view() for nodes. */ -function geo_citation_article_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) { +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 = ''; + $article_type = $entity->field_article_type->getValue(); + // If it's not a GEO Original article, there's nothing for us to do. + if (!isset($article_type[0]) || $article_type[0]['target_id'] != '303') { + return $markup; + } + + $authors = $entity->field_authors->getValue(); + $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 .= '
'; + + $tooltip = t("When citing this article, please use the following format:") + $markup .= '

' . t("Citations") . '

'; + + $markup .= '

'; + $markup .= "$authors ($year).  $title"; + if ($subtitle) { + $markup .= ":  $subtitle"; + } + $markup .= ".  "; + $markup .= t("Grassroots Economic Organizing (GEO)."); + $markup .= "  $url"; + $markup .= '

'; + + $markup .= '
'; + + return $markup; +}