argue-2.0.0-alpha4/modules/argue_structure/argue_structure.module

modules/argue_structure/argue_structure.module
<?php

/**
 * @file
 * Contains argue_structure.module.
 */

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\EntityInterface ;
use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeInterface;
use Drupal\Core\Url;
use Drupal\views\Views;
use Drupal\argue_proscons\Entity\ArgumentInterface;
use Drupal\Core\Cache\Cache;

/**
 * Implements hook_help().
 */
function argue_structure_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the argue_structure module.
    case 'help.page.argue_structure':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Argue basic structure of rule tree and user groups.') . '</p>';
      return $output;

    default:
      return '';
  }
}

/**
 * Implements hook_theme().
 */
function argue_structure_theme() {

  return [
    'argue_structure_list' => [
      'variables' => [
        'attributes' => NULL,
        'content' => NULL,
      ]
    ],
  ];
}



function argue_structure_theme_suggestions_form_alter(array &$suggestions, array $variables) {
  if (isset($variables["element"]["#form_id"])) {
    $suggestions[] = 'form__' . $variables["element"]["#form_id"];
  }
}

/**
 * Implements hook_preprocess_page_title().
 *
 * Overwrite title of section tree page (home) with the Argue project title.
 */
function argue_structure_preprocess_page_title(&$variables) {
  $route_name = \Drupal::routeMatch()->getRouteName();
  if ($route_name === 'view.argue_section_tree.section_page') {
    $query = \Drupal::request()->query;
    $node_type = $query->get('node_type', 'all');
    $title = &$variables['title']["#markup"];
    switch ($node_type) {
      case 'problem':
        $title = t('Problems brought up');
        break;
      case 'rule':
        $ratified = (int) $query->get('ratified', '0');
        $title = ($ratified)
          ? t('Ratified Rules (Preview)')
          : \Drupal::configFactory()->get('argue_structure.arguestructureconf')
            ->get('title_section_term_overview_page');
        break;
      default:
    }
  }
}

/**
 * Implements hook_entity_view_alter().
 *
 * Get the argue meta information.
 */
function argue_structure_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
  module_load_include('meta_properties.inc', 'argue_structure');
  $build['argue_meta'] = argue_structure_meta_properties_load($entity, $display);
}

/**
 * Implements hook_ENTITY_TYPE_view_alter()
 *
 * Attach view of rules handlung this problem to the problem (view_mde: full).
 */
function argue_structure_node_view_alter(array &$build, NodeInterface $node, EntityViewDisplayInterface $display) {
  if ($build['#view_mode'] == 'full' && $node->bundle() == 'problem') {
    $nid = $node->id();
    $build['rules_view'] = [
      '#type' => 'view',
      '#name' => 'argue_rules_ref_this_problem',
      '#display_id' => 'embed_1',
      '#arguments' => [$nid],
      '#embed' => TRUE,
    ];
    if (isset($build["field_comments"]["#weight"])) {
      $build['rules_view']['#weight'] = $build["field_comments"]["#weight"] - 1;
    }
  }
}


/**
 * Implements hook_ENTITY_TYPE_links_alter()
 * @Todo To be removed
 *   Action should be attached to field header (like add Change request).
 *
 * Adding action link at problem node to add a rule.
 */
function argue_structure_node_links_alter(array &$links, EntityInterface $entity, array $context) {
  if($context['view_mode'] == 'full' && $entity->bundle() == 'problem') {
    if(\Drupal::currentUser()->hasPermission('create rule content')) {
      $text = t('Add rule handling this problem');
      $url = Url::fromRoute('node.add', ['node_type' => 'rule']);
      $field_section = $entity->get('field_sector')->getValue();
      $sector = count($field_section) ? $field_section[0]['target_id'] : 0;
      $url->setOption('query', [
        'causal_problem' => $entity->id(),
        'sector' => $sector
      ]);
      $links['node']['#links']['add_rule'] = [
        'title' => $text,
        'url' => $url
      ];
    }
  }
}


/**
 * Implements hook_form_FORM_ID_alter().
 */
function argue_structure_form_taxonomy_term_sections_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $request = \Drupal::request();
  $sector = $request->get('sector');
  if($sector
    && isset($form["relations"]["parent"])
    && \Drupal::routeMatch()->getRouteName() == 'entity.taxonomy_term.add_form'
  ) {
    $form["relations"]["parent"]["#default_value"] = [$sector];
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function argue_structure_form_user_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if(isset($form['account']['name']['#title'])) {
    $form['account']['name']['#title'] = t('First and last name');
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Insert default values when creating a rule from action link at problem node.
 * see above: argue_structure_node_links_alter()
 */
function argue_structure_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if(in_array($form_id, ['node_problem_form', 'node_rule_form'])) {
    $request = \Drupal::request();

    $sector = $request->get('sector');
    if($sector && empty($form["field_sector"]["widget"]["#default_value"])) {
      $form["field_sector"]["widget"]["#default_value"] = [$sector];
    }

    /* Set causal problem from get param, if param set. See link set in
       argue_structure_node_links_alter(). */
    $causal_problem = $request->get('causal_problem');
    if($causal_problem && empty($form["field_causal_problems"]["widget"][0]["target_id"]["#default_value"])) {
      $causal_problem = \Drupal\node\Entity\Node::load($causal_problem);
      $form["field_causal_problems"]["widget"][0]["target_id"]["#default_value"] = $causal_problem;
    }

    // Remove field_ratified if user has no permission.
    if(isset($form["field_ratified"]) && !\Drupal::currentUser()->hasPermission('set rule ratified') ) {
      unset($form["field_ratified"]);
    }
  } elseif ($form_id == 'node_rule_edit_form') {
    // Disable field_ratified if user has no permission.
    if(isset($form["field_ratified"]) && !\Drupal::currentUser()->hasPermission('set rule ratified') ) {
      $form["field_ratified"]['#disabled'] = TRUE;
    }
  }
}

/**
 * Implements hook_ENTITY_TYPE_presave().
 *
 * Removes node creator on node publish.
 */
function argue_structure_node_presave(NodeInterface $node) {
  if ((bool) $node->getOwnerId() && $node->isPublished()) {
    $node_types = \Drupal::config('argue_structure.arguestructureconf')->get('unset_node_owner');
    if (isset($node_types[$node->bundle()]) && ($node_types[$node->bundle()] == $node->bundle())) {
      $node->setOwnerId(0);
    }
  }
}

/**
 * Implements hook_entity_presave().
 *
 * Invalidate cache tag for parent meta properties.
 */
function argue_structure_argument_presave(EntityInterface $entity) {
  // Invalidate cache tag for parent meta properties.
  if ($entity->isNew() && $nid = $entity->getReferenceId()) {
    Cache::invalidateTags(["node:{$nid}:meta:argument"]);
  }
}

/**
 * Implements hook_ENTITY_TYPE_predelete().
 *
 * Invalidate cache tag for parent meta properties.
 */
function argue_structure_argument_predelete(\Drupal\argue_proscons\Entity\ArgumentInterface $argument) {
  if ($nid = $argument->getReferenceId()) {
    Cache::invalidateTags(["node:{$nid}:meta:argument"]);
  }
}

/**
 * Implements hook_argue_meta_patch_patch().
 */
function argue_structure_argue_meta_patch_patch(\Drupal\change_requests\Entity\Patch $patch, $view_mode): array
{
  $result = [];
  if ($voting = _argue_structure_argue_meta_voting_rate($patch)) {
    $result['voting'] = [
      '#theme' => 'argue_chip',
      '#attributes' => [
        'class' => ['voting'],
        'title' => t('Voting result.')
      ],
      '#icon_before' => 'vote_result',
      '#text' => $voting,
      '#icon_after' => NULL,
      '#cache' => ['tags' => ["patch:{$patch->id()}:meta:voting"]],
    ];
  }

  // Patch status.
  $statusId = $patch->getStatus();
  $status_text = $patch->getStatus(true);
  $result['status'] = [
    '#theme' => 'argue_chip',
    '#attributes' => [
      'class' => ['chip--cr-status', $statusId],
      'title' => t('Workflow status of change request: %status.', ['%status' => $status_text])
    ],
    '#icon_before' => $statusId,
    '#text' => $status_text,
    '#icon_after' => NULL,
    '#cache' => ['tags' => ["patch:{$patch->id()}:meta:status"]],
  ];
  return $result;
}


/**
 * Implements hook_argue_meta_patch_patch().
 */
function _argue_structure_argue_meta_voting(EntityInterface $entity): array {
  $entity_type_id = $entity->getEntityTypeId();
  $entity_id = $entity->id();
  /** @var \Drupal\votingapi\VoteResultFunctionManager $vote_result_manager */
  $vote_result_manager = \Drupal::service('plugin.manager.votingapi.resultfunction');
  return $vote_result_manager->getResults($entity_type_id, $entity_id);
}

/**
 * @param \Drupal\Core\Entity\EntityInterface $entity
 *
 * @return string|null
 */
function _argue_structure_argue_meta_voting_rate(EntityInterface $entity): ?string {
  $result = _argue_structure_argue_meta_voting($entity);
  if (isset($result["updown"])) {
    $result = $result["updown"];
    $up = (int) $result['rate_count_up'];
    $down = $result['vote_count'] - $up;
    $string = "{$up}:{$down}";
    return $string;
  }
  else return NULL;
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc