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

Add option to include "from" parent in Nav tag output #2107

Closed
alwaysblank opened this Issue Aug 7, 2018 · 1 comment

Comments

Projects
None yet
3 participants
@alwaysblank

alwaysblank commented Aug 7, 2018

Is your feature request related to a problem? Please describe.
Adding a "Parent" item (i.e. the one specified in "from") to a Nav tag is difficult. It would be nice if it were simple.

Describe the solution you'd like
I would like to use the Nav tag to create secondary navigation elements (i.e. tabs) that include the "Parent" in addition to its children.

Describe alternatives you've considered
Existing options for adding additional items to Nav tag content are generally more complex than I want to deal with for a simple thing. I could also manually generated small lists w/ URL checks and accomplish the same thing.

Additional context
The Nav tag already includes a very similar feature to the one I want: If you pass include_home, the home page of your site will be added to the list of items.

Would you be willing to sponsor this feature?
I don't have the budget to sponsor financially, but below you can find a small patch that enables this feature (it essentially duplicated the include_home functionality, but for arbitrary endpoints).

From 53ed90724e326e4d82545f383e95786f20142a4c Mon Sep 17 00:00:00 2001
From: Ben Martinez-Bateman <ben@alwaysblank.org>
Date: Thu, 2 Aug 2018 15:51:39 -0700
Subject: [PATCH] Add include_root option to Nav tag

This adds a boolean option include_root to the Nav tag. It
works almost identically to the include_home option, except
that it forces includes of the item indicated by the from
parameter.
---
 statamic/bundles/Nav/NavTags.php     |  1 +
 statamic/bundles/Nav/TreeFactory.php | 24 ++++++++++++++++++++++++
 2 files changed, 25 insertions(+)

diff --git a/statamic/bundles/Nav/NavTags.php b/statamic/bundles/Nav/NavTags.php
index e055d25..b4cda69 100644
--- a/statamic/bundles/Nav/NavTags.php
+++ b/statamic/bundles/Nav/NavTags.php
@@ -105,6 +105,7 @@ class NavTags extends Tags
             'entries'      => $this->getBool('include_entries', false),
             'sort'         => $this->get('sort'),
             'include_home' => $this->getBool('include_home'),
+            'include_root' => $this->getBool('include_root'),
             'exclude'      => $this->getList('exclude'),
             'conditions'   => $this->getConditionParameters(),
             'locale'       => $this->get('locale', site_locale()),
diff --git a/statamic/bundles/Nav/TreeFactory.php b/statamic/bundles/Nav/TreeFactory.php
index 3fec7dd..5e54a4d 100644
--- a/statamic/bundles/Nav/TreeFactory.php
+++ b/statamic/bundles/Nav/TreeFactory.php
@@ -57,6 +57,10 @@ class TreeFactory
             $tree_content = $this->prependHome($tree_content);
         }
 
+        if ($this->params['include_root']) {
+            $tree_content = $this->prependRoot($tree_content);
+        }
+
         $tree_content = $this->limitAndOffset($tree_content);
 
         $tree = new Tree($tree_content);
@@ -86,6 +90,26 @@ class TreeFactory
         return array_reverse($tree_content);
     }
 
+    /**
+     * Add the root "from" page to the start of the array
+     *
+     * @param array $output
+     * @return array
+     */
+    private function prependRoot($tree_content)
+    {
+        $root = [
+            'page'     => Page::whereUri($this->params['from'])->in($this->params['locale'])->get(),
+            'depth'    => 1,
+            'children' => []
+        ];
+
+        $tree_content = array_reverse($tree_content);
+        $tree_content[] = $root;
+
+        return array_reverse($tree_content);
+    }
+
     private function limitAndOffset($items)
     {
         $offset = array_get($this->params, 'offset');
-- 
2.10.5
@jasonvarga

This comment has been minimized.

Member

jasonvarga commented Oct 31, 2018

Sorry for the delay, thanks!

@jackmcdade jackmcdade closed this Nov 6, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment