Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DP-36072: Add option to hide types in hierarchy #2789

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

namespace Drupal\mass_hierarchy\Form;

use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\RedirectCommand;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\entity_hierarchy\Form\HierarchyChildrenForm as EntityHierachyHierarchyChildrenForm;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

Expand All @@ -22,6 +26,27 @@ public function form(array $form, FormStateInterface $form_state) {

$form['#attached']['library'][] = 'entity_hierarchy/entity_hierarchy.nodetypeform';

// Retrieve the current query string parameter value, defaulting to FALSE.
$current_hide = \Drupal::request()->query->get('hide-types', FALSE);

// Add the "Hide specific types" checkbox with AJAX functionality.
$form['hide_specific_types'] = [
'#type' => 'checkbox',
'#title' => $this->t('Hide people, contacts, events, news'),
'#description' => $this->t('Check this box to hide specific types from the hierarchy view.'),
'#default_value' => $current_hide,
'#ajax' => [
'callback' => '::redirectWithQuery',
'wrapper' => 'hierarchy-children-form-wrapper',
'effect' => 'fade',
],
];

// Add a wrapper to the entire form to enable AJAX updates.
$form['#prefix'] = '<div id="hierarchy-children-form-wrapper">';
$form['#suffix'] = '</div>';


/** @var \Drupal\Core\Field\FieldDefinitionInterface[] $fields */
$fields = $this->parentCandidate->getCandidateFields($this->entity);
if (!$fields) {
Expand Down Expand Up @@ -133,9 +158,9 @@ public function form(array $form, FormStateInterface $form_state) {
$ids[] = $node->getId();
}

if (!empty($ids)) {
$pageviews = \Drupal::service('bigquery.storage')->getRecords($ids);
}
// if (!empty($ids)) {
// $pageviews = \Drupal::service('bigquery.storage')->getRecords($ids);
// }

foreach ($children as $weight => $node) {
if (!$childEntities->contains($node)) {
Expand All @@ -150,6 +175,14 @@ public function form(array $form, FormStateInterface $form_state) {
continue;
}

dump(\Drupal::request()->query);
dump($current_hide);
if ($current_hide) {
if (in_array($childEntity->bundle(), ['person', 'contact_information', 'event', 'news'])) {
continue;
}
}

if (!$childEntity->isPublished()) {
continue;
}
Expand Down Expand Up @@ -254,6 +287,27 @@ public function form(array $form, FormStateInterface $form_state) {
return $form;
}

/**
* AJAX callback to handle the query string and redirect.
*/
public function redirectWithQuery(array &$form, FormStateInterface $form_state) {
// Get the checkbox value.
$hide_specific_types = $form_state->getValue('hide_specific_types', FALSE);

if ($hide_specific_types) {
$url = Url::fromRoute('<current>', [], [
'query' => ['hide-types' => $hide_specific_types ? 1 : 0],
]);
}
else {
$url = Url::fromRoute('<current>');
}

$response = new AjaxResponse();
$response->addCommand(new RedirectCommand($url->toString()));
return $response;
}

/**
* {@inheritdoc}
*/
Expand Down