zaya-1.0.x-dev/zaya.module

zaya.module
<?php

/**
 * @file
 * Primary module hooks for zaya module.
 */

use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\group\Entity\GroupMembership;
use Drupal\group\Entity\GroupRelationship;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;

/**
 * Implements hook_entity_bundle_info_alter().
 */
function zaya_entity_bundle_info_alter(array &$bundles): void {
  if (isset($bundles['node']['zaya_progress'])) {
    // phpcs:ignore Drupal.Classes.FullyQualifiedNamespace.UseStatementMissing
    $bundles['node']['zaya_progress']['class'] = \Drupal\zaya\Entity\Node\ZayaProgress::class;
  }
  // Relationship class bundle for chapter.
  if (isset($bundles['group_relationship']['zaya_itinerary-07d5825beb2b103c3'])) {
    // phpcs:ignore Drupal.Classes.FullyQualifiedNamespace.UseStatementMissing
    $bundles['group_relationship']['zaya_itinerary-07d5825beb2b103c3']['class'] = \Drupal\zaya\Entity\GroupRelationship\ZayaItinerary07d5825beb2b103c3::class;
  }
  // Relationship class bundle for resource.
  if (isset($bundles['group_relationship']['zaya_itinerary-98b49b8bac6eb9f06'])) {
    // phpcs:ignore Drupal.Classes.FullyQualifiedNamespace.UseStatementMissing
    $bundles['group_relationship']['zaya_itinerary-98b49b8bac6eb9f06']['class'] = \Drupal\zaya\Entity\GroupRelationship\ZayaItinerary98b49b8bac6eb9f06::class;
  }
  // Group class bundle for itinerary.
  if (isset($bundles['group']['zaya_itinerary'])) {
    // phpcs:ignore Drupal.Classes.FullyQualifiedNamespace.UseStatementMissing
    $bundles['group']['zaya_itinerary']['class'] = \Drupal\zaya\Entity\Group\ZayaItinerary::class;
  }
}

/**
 * Implements hook_ENTITY_TYPE_view_alter() for node entities.
 */
function zaya_node_view_alter(array &$build, EntityInterface $node, EntityViewDisplayInterface $display) {
  if ($node->isNew() || isset($node->in_preview)) {
    return;
  }
  // Update the history table, stating that this user viewed this node.
  if ($display->getOriginalMode() === 'full') {
    $build['#cache']['contexts'][] = 'user.roles:authenticated';
  }
}

/**
 * Implements hook_ENTITY_TYPE_view().
 */
function zaya_group_relationship_view(array &$build, EntityInterface $group_relationship, EntityViewDisplayInterface $display, $view_mode) {
  if ($group_relationship->isNew() || isset($group_relationship->in_preview)) {
    return;
  }
  // Only do the extra work if the component is configured to be displayed.
  // This assumes a extra field has been defined for the
  // entity bundle in
  // hook_entity_extra_field_info -> namely zaya_entity_extra_field_info.
  if ($group_relationship instanceof FieldableEntityInterface && $display->getComponent('zaya_relationship_links')) {
    $build['#cache']['contexts'][] = 'user.roles:authenticated';
    if (\Drupal::currentUser()->isAuthenticated()) {
      $build['zaya_relationship_links'] = [
        '#type' => 'container',
        '#attributes' => [
          'class' => [
            'zaya-relationship-links',
          ],
        ],
        'dependencies' => [
          '#type' => 'container',
          '#attributes' => [
            'class' => [
              'dependencies-relationship-links',
            ],
          ],
        ],
        'dependents' => [
          '#type' => 'container',
          '#attributes' => [
            'class' => [
              'dependents-relationship-links',
            ],
          ],
        ],
      ];
      if ($dependencies = $group_relationship->getDependencyNodeRelationships()) {
        $bundle = $group_relationship->getEntity()->type->entity;
        foreach ($dependencies as $dep_rel) {
          if ($dep_rel->getEntity()->access('view')) {
            $dep_rel_link = $dep_rel->toLink()->toRenderable();
            $dep_rel_link['#attributes'] = [
              'class' => ['dependency-as-relationship'],
              'rel' => 'prev',
              'title' => t('Go to previous @type', ['@type' => $bundle->label()]),
            ];
            $build['zaya_relationship_links']['dependencies'][] = $dep_rel_link;
          }
        }
        if (isset($build['zaya_relationship_links']['dependencies'][0])) {
          $build['zaya_relationship_links']['dependencies']['label'] = [
            '#type' => 'container',
            '#markup' => t('Go to previous @type', ['@type' => $bundle->label()]),
            '#attributes' => ['class' => ['label']],
          ];
        }
      }
      if ($dependents = $group_relationship->getDependentNodeRelationships()) {
        $bundle = $group_relationship->getEntity()->type->entity;
        foreach ($dependents as $next_rel) {
          if ($next_rel->getEntity()->access('view')) {
            $next_rel_link = $next_rel->toLink()->toRenderable();
            $next_rel_link['#attributes'] = [
              'class' => ['dependent-as-relationship'],
              'rel' => 'next',
              'title' => t('Go to next @type', ['@type' => $bundle->label()]),
            ];
            $build['zaya_relationship_links']['dependents'][] = $next_rel_link;
          }
        }
        if (isset($build['zaya_relationship_links']['dependents'][0])) {
          $build['zaya_relationship_links']['dependents']['label'] = [
            '#type' => 'container',
            '#markup' => t('Go to next @type', ['@type' => $bundle->label()]),
            '#attributes' => ['class' => ['label']],
          ];
        }
      }
    }
  }
}

/**
 * Implements hook_ENTITY_TYPE_view_alter() for group_relationship entities.
 */
function zaya_group_relationship_view_alter(array &$build, EntityInterface $group_relationship, EntityViewDisplayInterface $display) {
  if ($group_relationship->isNew() || isset($group_relationship->in_preview)) {
    return;
  }
  // Update the history table, stating that this user viewed this
  // group_relationship.
  if ($display->getOriginalMode() === 'full') {
    $build['#cache']['contexts'][] = 'user.roles:authenticated';
  }
}

/**
 * Implements hook_entity_base_field_info_alter().
 */
function zaya_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
  // Relationship class bundle for chapter.
  // @todo seat for dependencies rendering.
  // dpm($fields);
  /*dpm($entity_type->id());
  if ($entity_type->id() === "group_relationship") {
  $fields['chapter_resources'] =
  \Drupal\Core\Field\BaseFieldDefinition::create('string')
  ->setName('my_computed_field')
  ->setLabel(t('My computed field'))i
  ->setComputed(TRUE)
  ->setClass('\Drupal\zaya\ChapterRelationshipChapterResourcesMapped');
  }*/
}

/**
 * Implements hook_entity_extra_field_info().
 */
function zaya_entity_extra_field_info() {
  $extra = [];
  foreach (GroupRelationship::loadMultiple() as $bundle) {
    if ($bundle->getPluginId() === 'group_node:zaya_chapter') {
      $extra['group_relationship'][$bundle->getRelationshipType()->id()]['display']['zaya_relationship_links'] = [
        'label' => t('Relationship links'),
        'description' => t('Relationship links'),
        'weight' => 100,
        'visible' => TRUE,
      ];
    }
  }
  return $extra;
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function zaya_form_node_form_alter(&$form, &$form_state, $form_id) {
  if (TRUE) {
    // Unset by now as referenced entities will get inherited group directly
    // using module group_relationship_inheritance add operations buttons to
    // link to create resource in chapters add/edit page.
    return;
  }
  if ($form_id === 'node_zaya_chapter_form' or $form_id === 'node_zaya_chapter_edit_form') {
    $itineraries = [];
    $operations = [];
    if (\Drupal::routeMatch()->getParameter('group')) {
      $itineraries[] = \Drupal::routeMatch()->getParameter('group');
    }

    if (empty($itineraries)) {

      $memberships = GroupMembership::loadByUser(\Drupal::currentUser());
      foreach ($memberships as $membership) {
        $itineraries[] = $membership->getGroup();
      }
    }
    foreach ($itineraries as $relatable_itinerary) {
      /** @var \Drupal\group\Plugin\Group\Relation\GroupRelationTypeManagerInterface **/
      $plugin_manager = \Drupal::service('group_relation_type.manager');
      // assert($relationship instanceof GroupRelationInterface);.
      // ->getRelationTypeId());
      $operation_provider = $plugin_manager->getOperationProvider("group_node:zaya_resource");
      $operations_pre = $operation_provider->getGroupOperations($relatable_itinerary);
      foreach ($operations_pre as $id => $op_pre) {

        $operations_pre[$id]['title'] = t("Create a @type in @group", $op_pre['title']->getArguments() + ["@group" => $relatable_itinerary->label()], $op_pre['title']->getOptions());
        $operations_pre[$id]['url']->setOptions([
          'attributes' => [
            'target' => '_blank',
          /*        'class' => ['use-ajax'],
        'data-dialog-type' => 'modal',
        'data-dialog-options' => Json::encode([
          'width' => 700,
        ])*/
          ],
        ]);
        $operations[] = $operations_pre[$id];
      }

    }

    $form['zaya_chapter_resources_links_wrapper'] = [
      '#type' => 'container',
      '#weight' => $form['zaya_chapter_resources']['#weight'] + 1,
      '#attributes' => ['class' => ['webform-message']],
    ];
    $form['zaya_chapter_resources_links_wrapper']['zaya_chapter_resources_links'] = [
      '#type' => 'operations',
      '#links' => $operations,
      '#prefix' => t("Resources must first be created before referencing them.") . "<br/>",
      // '#attached' => ['library' => ['core/drupal.dialog.ajax']],
    ];
    // $form['zaya_chapter_resources']['widget']['#description'] = t("Please add the resources in some of these groups, and then get back to select here:");//. \Drupal::service('renderer')->renderRoot($form['zaya_chapter_resources_links']);
  }
}

/**
 * Implements hook_views_data_alter().
 */
function zaya_views_data_alter(&$data) {
  // We want that fields with entity reference revisions to be
  // less strict and do not expect revision id to match.
  $no_rev_rel = $data["node__zaya_chapter_resources"]["zaya_chapter_resources"];
  $no_rev_rel['relationship']["base field"] = "nid";
  $no_rev_rel['relationship']["relationship field"] = "zaya_chapter_resources_target_id";
  $no_rev_rel['relationship']["help"] .= t("(without version match)");
  $no_rev_rel['relationship']["label"] .= t("(without version match)");
  $data["node__zaya_chapter_resources"]["zaya_chapter_resources_no_revisions"] = $no_rev_rel;
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Allows to add entity form edit link to group relationship form.
 */
function zaya_form_group_relationship_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (isset($form_state->getBuildInfo()['base_form_id']) && $form_state->getBuildInfo()['base_form_id'] === 'group_relationship_form') {
    if (!$form_state->getFormObject()->getEntity()->isNew()) {
      $entity = $form_state->getFormObject()->getEntity()->getEntity();
      $url = $entity->toUrl('edit-form');
      $group = $form_state->getFormObject()->getEntity()->getGroup();
      $relationship_url = $form_state->getFormObject()->getEntity()->toUrl();
      $url->setOption('query', ['destination' => $relationship_url->toString()]);
      $link = Link::fromTextAndUrl(t('edit the @content', ["@content" => $entity->getEntityTypeId()]), $url);
      $form['related_entity_link'] = $link->toRenderable();
      $form['related_entity_link']['#prefix'] = t('You are currently editing the relationship of %label with group %group, instead of this you also could', [
        '%label' => $entity->label(),
        '%group' => $group->label(),
      ]) . " ";
    }
  }
}

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

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