argue-2.0.0-alpha4/modules/argue_proscons/argue_proscons.module
modules/argue_proscons/argue_proscons.module
<?php
/**
* @file
* Contains argue_proscons.module.
*/
use Drupal\argue_proscons\Entity\ArgumentInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\node\NodeInterface;
use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
/**
* Implements hook_help().
*/
function argue_proscons_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the argue_proscons module.
case 'help.page.argue_proscons':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Provides a subentity list with arguments for a rule represented by a node.') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_theme().
*/
function argue_proscons_theme() {
return [
'argue_proscons' => [
'render element' => 'content',
],
'argue_proscons__header' => [
'variables' => [
'attributes' => NULL,
'type' => NULL,
'label' => NULL,
],
],
'argument' => [
'render element' => 'content',
],
// @todo Can this be removed?? Better done by theme hook suggestion.
'argument__teaser' => [
'render element' => 'content',
],
// @todo Can this be removed?? Better done by theme hook suggestion.
'argument__teaser_pro' => [
'render element' => 'content',
],
'change_request__list_item' => [
'variables' => [
'attributes' => NULL,
'url' => NULL,
'label' => NULL,
'status' => NULL,
],
],
'argue_chip_set' => [
'render element' => 'children',
],
'argue_chip' => [
'variables' => [
'attributes' => NULL,
'icon_before' => NULL,
'text' => '',
'icon_after' => NULL,
],
],
];
}
/**
* Implements hook_argue_meta_ENTITY_TYPE()
*
* Load the meta properties of current entity.
*/
function argue_proscons_argue_meta_node_rule(NodeInterface $node, $view_mode) {
$nid = $node->id();
$cache_tag = "node:{$nid}:meta:argument";
/** @var \Drupal\argue_proscons\EvaluatingService $eval */
$eval = \Drupal::service('argue_proscons.evaluating_service');
$argCount = $eval->getRuleArgumentCounts($nid);
$result = [
'argue_pro_con' => [
'#theme' => 'argue_chip',
'#attributes' => ['title' => t('Number of arguments (pro:contra)')],
'#icon_before' => 'argue_pro_con',
'#text' => "{$argCount['pro']}:{$argCount['con']}",
'#icon_after' => NULL,
'#cache' => ['tags' => [$cache_tag]],
]
];
return $result;
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function argue_proscons_theme_suggestions_argument(array $variables) {
$suggestions = [];
$sanitized_view_mode = strtr($variables['content']['#view_mode'], '.', '_');
$suggestions[] = 'argument__' . $sanitized_view_mode;
return $suggestions;
}
/**
* Implements hook_ENTITY_TYPE_view().
*
* The canonical view of arguments will be redirected to its parent node.
*
* @param array $build
* @param \Drupal\Core\Entity\EntityInterface $entity
* @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
* @param $view_mode
*
* @throws \Drupal\Core\Entity\EntityMalformedException
*/
function argue_proscons_argument_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
if($view_mode == 'full' && \Drupal::routeMatch()->getRouteName() == 'entity.argument.canonical') {
$parent = $entity->get("reference_id")->referencedEntities();
/** @var \Drupal\node\NodeInterface $parent */
if($parent = reset($parent)) {
$url = $parent->toUrl();
$url->setOption('fragment' ,'argument_' . $entity->id());
$url = (string) $url->toString();
$response = new RedirectResponse($url, 301);
$response->send();
exit();
}
} else {
// Set id to scroll to the id.
if(!isset($build['attributes'])) {
$build['#attributes'] = new \Drupal\Core\Template\Attribute();
}
$arg_type = $entity->getTypeStr();
$build['#attributes']->setAttribute('id', 'argument_' . $entity->id());
$build['#attributes']->addClass('argument-' . $arg_type);
$build['argument_type'] = ['#markup' => $arg_type];
}
// Operation links as argue drop menu.
if (isset($build['operations'])) {
$drop_menu = [
'#theme' => 'drop_menu',
'#id' => 'arguement__drop_menu_' . $entity->id(),
'#attributes' => [
'class' => ['drop-menu__top_left'],
'role' => 'navigation',
],
'#links' => []
];
foreach ($build["operations"]["#links"] as $link) {
$drop_menu['#links'][] = [
'#type' => 'link',
'#title' => $link['title'],
'#url' => $link['url'],
'#weight' => $link['weight'] ?? 0,
];
}
$build["operations"] = $drop_menu;
}
// Attach history timestamp to mark as new.
$build['new_indicator_timestamp'] = [
'#markup' => $entity->get('changed')->getString()
];
// Attach the rate_vote_widget.
// @todo Replace by own rate widget.
$type = $entity->getEntityTypeId();
$bundle = $entity->bundle();
$view_string = "{$type}__{$bundle}__{$view_mode}";
$show = [
'argument__argument__teaser',
'argument__argument__teaser_pro',
];
if (in_array($view_string, $show) && $widget_config = $display->getComponent('rate_vote_widget')) {
$vote_widget_service = \Drupal::service('rate.entity.vote_widget');
$vote_widget = $vote_widget_service->buildRateVotingWidget($entity->id(), $type, $bundle);
if (isset($vote_widget['rate_vote_widget'])) {
$vote_widget['rate_vote_widget']['#weight'] = $widget_config['weight'] ?? 2;
}
$vote_widget['#group'] = 'voting';
// move voting widget to own region.
$build['voting'] = [
'#type' => 'container',
'#weight' => 10,
'#attributes' => ['class' => ['voting']],
'#tree' => TRUE,
'#access' => TRUE,
'votingapi_links' => $vote_widget,
];
}
}
/**
* Implements hook_entity_view_mode_alter()
*
* For Arguments, change the view mode when it is teaser.
* (Arguments have no bundles.)
*/
function argue_proscons_entity_view_mode_alter(&$view_mode, Drupal\Core\Entity\EntityInterface $entity, $context) {
if ($entity->getEntityTypeId() == 'argument' && $view_mode == 'teaser') {
if($entity->getTypeStr() == 'pro') {
$view_mode = 'teaser_pro';
};
}
}
/**
* Implements hook_entity_form_display_alter().
*
* For Arguments, change the view mode when it is teaser.
* (Arguments have no bundles.)
*/
function argue_proscons_entity_form_display_alter(EntityFormDisplayInterface &$form_display, array $context) {
if ($context['entity_type'] == 'argument' && $context["form_mode"] == 'edit') {
/** @var \Drupal\Core\Entity\EntityDisplayRepository $entity_display_repo */
$entity_display_repo = \Drupal::service('entity_display.repository');
$form_mode_opts = $entity_display_repo->getFormModeOptionsByBundle('argument', 'argument');
/** @var \Drupal\argue_proscons\Entity\Argument $argument */
$argument = \Drupal::routeMatch()->getParameter('argument');
$type_str = $argument->getTypeStr();
if (isset($form_mode_opts["edit_{$type_str}"])) {
$form_display = $entity_display_repo->getFormDisplay('argument', 'argument', "edit_{$type_str}") ?: $form_display;
}
}
}
