How to get static page dropdown in OctoberCMS with get page tree?












0















I am open this based from my question in my own comment from Static page dropdown within static page in OctoberCMS.



I have problem when adding $pageList->getPageTree(true). The child pages did not display. My current code is repeating foreach from $pageObject->subpages which is not a good practice.



Below here is my code example:



Plugin.php



<?php namespace MyPluginCustomPlugin
use SystemClassesPluginBase;
public function boot() {
RainLabPagesClassesPage::extend(function($model) {
$model->addDynamicMethod('getPageOptions', function() {
$theme = CmsClassesTheme::getEditTheme();
$pageList = new RainLabPagesClassesPageList($theme);
$pages = ;
foreach ($pageList->getPageTree(true) as $name => $pageObject) {
$pages[$pageObject->page->url] = $pageObject->page->title;
if ($pageObject->subpages) {
foreach ($pageObject->subpages as $name => $pageObject) {
$pages[$pageObject->page->url] = '&nbsp;&nbsp;&nbsp;' . $pageObject->page->title;
if ($pageObject->subpages) {
foreach ($pageObject->subpages as $name => $pageObject) {
$pages[$pageObject->page->url] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . $pageObject->page->title;
}
}
}
}
}
return $pages;
});
});
}


Appreciate if anyone could help.










share|improve this question





























    0















    I am open this based from my question in my own comment from Static page dropdown within static page in OctoberCMS.



    I have problem when adding $pageList->getPageTree(true). The child pages did not display. My current code is repeating foreach from $pageObject->subpages which is not a good practice.



    Below here is my code example:



    Plugin.php



    <?php namespace MyPluginCustomPlugin
    use SystemClassesPluginBase;
    public function boot() {
    RainLabPagesClassesPage::extend(function($model) {
    $model->addDynamicMethod('getPageOptions', function() {
    $theme = CmsClassesTheme::getEditTheme();
    $pageList = new RainLabPagesClassesPageList($theme);
    $pages = ;
    foreach ($pageList->getPageTree(true) as $name => $pageObject) {
    $pages[$pageObject->page->url] = $pageObject->page->title;
    if ($pageObject->subpages) {
    foreach ($pageObject->subpages as $name => $pageObject) {
    $pages[$pageObject->page->url] = '&nbsp;&nbsp;&nbsp;' . $pageObject->page->title;
    if ($pageObject->subpages) {
    foreach ($pageObject->subpages as $name => $pageObject) {
    $pages[$pageObject->page->url] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . $pageObject->page->title;
    }
    }
    }
    }
    }
    return $pages;
    });
    });
    }


    Appreciate if anyone could help.










    share|improve this question



























      0












      0








      0








      I am open this based from my question in my own comment from Static page dropdown within static page in OctoberCMS.



      I have problem when adding $pageList->getPageTree(true). The child pages did not display. My current code is repeating foreach from $pageObject->subpages which is not a good practice.



      Below here is my code example:



      Plugin.php



      <?php namespace MyPluginCustomPlugin
      use SystemClassesPluginBase;
      public function boot() {
      RainLabPagesClassesPage::extend(function($model) {
      $model->addDynamicMethod('getPageOptions', function() {
      $theme = CmsClassesTheme::getEditTheme();
      $pageList = new RainLabPagesClassesPageList($theme);
      $pages = ;
      foreach ($pageList->getPageTree(true) as $name => $pageObject) {
      $pages[$pageObject->page->url] = $pageObject->page->title;
      if ($pageObject->subpages) {
      foreach ($pageObject->subpages as $name => $pageObject) {
      $pages[$pageObject->page->url] = '&nbsp;&nbsp;&nbsp;' . $pageObject->page->title;
      if ($pageObject->subpages) {
      foreach ($pageObject->subpages as $name => $pageObject) {
      $pages[$pageObject->page->url] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . $pageObject->page->title;
      }
      }
      }
      }
      }
      return $pages;
      });
      });
      }


      Appreciate if anyone could help.










      share|improve this question
















      I am open this based from my question in my own comment from Static page dropdown within static page in OctoberCMS.



      I have problem when adding $pageList->getPageTree(true). The child pages did not display. My current code is repeating foreach from $pageObject->subpages which is not a good practice.



      Below here is my code example:



      Plugin.php



      <?php namespace MyPluginCustomPlugin
      use SystemClassesPluginBase;
      public function boot() {
      RainLabPagesClassesPage::extend(function($model) {
      $model->addDynamicMethod('getPageOptions', function() {
      $theme = CmsClassesTheme::getEditTheme();
      $pageList = new RainLabPagesClassesPageList($theme);
      $pages = ;
      foreach ($pageList->getPageTree(true) as $name => $pageObject) {
      $pages[$pageObject->page->url] = $pageObject->page->title;
      if ($pageObject->subpages) {
      foreach ($pageObject->subpages as $name => $pageObject) {
      $pages[$pageObject->page->url] = '&nbsp;&nbsp;&nbsp;' . $pageObject->page->title;
      if ($pageObject->subpages) {
      foreach ($pageObject->subpages as $name => $pageObject) {
      $pages[$pageObject->page->url] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . $pageObject->page->title;
      }
      }
      }
      }
      }
      return $pages;
      });
      });
      }


      Appreciate if anyone could help.







      php dropdown octobercms static-pages






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 27 '18 at 3:55







      claudchan

















      asked Nov 26 '18 at 8:20









      claudchanclaudchan

      93112




      93112
























          1 Answer
          1






          active

          oldest

          votes


















          2














          may be this will help you.





          function onStart() {
          $theme = CmsClassesTheme::getEditTheme();
          $pageList = new RainLabPagesClassesPageList($theme);
          $treePageList = $pageList->getPageTree(true);
          $pages = ;
          $this->getRecursivePage($pages, $treePageList);
          dd($pages);
          }

          function getRecursivePage(&$pages, $subpages, $level = 0) {
          $level++;
          foreach($subpages as $pageArr) {
          $pages[$pageArr->page->url] =
          str_repeat('-',$level) . ' ' . $pageArr->page->title;
          if(count($pageArr->subpages) > 0) {
          $this->getRecursivePage($pages, $pageArr->subpages, $level);
          }
          }
          }



          Output






          array:9 [▼
          "/content" => "- Content"
          "/content/pages" => "-- Static Pages"
          "/content/content" => "-- Content"
          "/content/models" => "-- Models"
          "/content/urls" => "-- URLs"
          "/content/urls/tesets" => "--- tesets"
          "/test-sp" => "- test-sp"
          "/test-sp/oks" => "-- oks"
          "/test" => "- test"
          ]


          here you can just replace - to &nbsp; or just remove that part [ I added because you had that in code so, may be useful to you. ]




          For your code






          public function boot() {
          RainLabPagesClassesPage::extend(function($model) {
          $model->addDynamicMethod('getPageOptions', function() {
          $theme = CmsClassesTheme::getEditTheme();
          $pageList = new RainLabPagesClassesPageList($theme);
          $treePageList = $pageList->getPageTree(true);
          $pages = ;
          $this->getRecursivePage($pages, $treePageList);
          return $pages;
          });
          });
          }

          public function getRecursivePage(&$pages, $subpages, $level = 0) {
          $level++;
          foreach($subpages as $pageArr) {
          $pages[$pageArr->page->url] =
          str_repeat('-',$level) . ' ' . $pageArr->page->title;
          if(count($pageArr->subpages) > 0) {
          $this->getRecursivePage($pages, $pageArr->subpages, $level);
          }
          }
          }


          If any doubt please comment.






          share|improve this answer


























          • Hi, the code seems does not work after subpages. imgur.com/a/0Tazzmm All the parent pages becomes child, after the subpages loop

            – claudchan
            Nov 27 '18 at 2:31













          • hmm, updated getRecursivePage() method please use new one now it should work.

            – Hardik Satasiya
            Nov 27 '18 at 7:30








          • 1





            Great! It works and thank you!

            – claudchan
            Nov 27 '18 at 9:26











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53477068%2fhow-to-get-static-page-dropdown-in-octobercms-with-get-page-tree%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          may be this will help you.





          function onStart() {
          $theme = CmsClassesTheme::getEditTheme();
          $pageList = new RainLabPagesClassesPageList($theme);
          $treePageList = $pageList->getPageTree(true);
          $pages = ;
          $this->getRecursivePage($pages, $treePageList);
          dd($pages);
          }

          function getRecursivePage(&$pages, $subpages, $level = 0) {
          $level++;
          foreach($subpages as $pageArr) {
          $pages[$pageArr->page->url] =
          str_repeat('-',$level) . ' ' . $pageArr->page->title;
          if(count($pageArr->subpages) > 0) {
          $this->getRecursivePage($pages, $pageArr->subpages, $level);
          }
          }
          }



          Output






          array:9 [▼
          "/content" => "- Content"
          "/content/pages" => "-- Static Pages"
          "/content/content" => "-- Content"
          "/content/models" => "-- Models"
          "/content/urls" => "-- URLs"
          "/content/urls/tesets" => "--- tesets"
          "/test-sp" => "- test-sp"
          "/test-sp/oks" => "-- oks"
          "/test" => "- test"
          ]


          here you can just replace - to &nbsp; or just remove that part [ I added because you had that in code so, may be useful to you. ]




          For your code






          public function boot() {
          RainLabPagesClassesPage::extend(function($model) {
          $model->addDynamicMethod('getPageOptions', function() {
          $theme = CmsClassesTheme::getEditTheme();
          $pageList = new RainLabPagesClassesPageList($theme);
          $treePageList = $pageList->getPageTree(true);
          $pages = ;
          $this->getRecursivePage($pages, $treePageList);
          return $pages;
          });
          });
          }

          public function getRecursivePage(&$pages, $subpages, $level = 0) {
          $level++;
          foreach($subpages as $pageArr) {
          $pages[$pageArr->page->url] =
          str_repeat('-',$level) . ' ' . $pageArr->page->title;
          if(count($pageArr->subpages) > 0) {
          $this->getRecursivePage($pages, $pageArr->subpages, $level);
          }
          }
          }


          If any doubt please comment.






          share|improve this answer


























          • Hi, the code seems does not work after subpages. imgur.com/a/0Tazzmm All the parent pages becomes child, after the subpages loop

            – claudchan
            Nov 27 '18 at 2:31













          • hmm, updated getRecursivePage() method please use new one now it should work.

            – Hardik Satasiya
            Nov 27 '18 at 7:30








          • 1





            Great! It works and thank you!

            – claudchan
            Nov 27 '18 at 9:26
















          2














          may be this will help you.





          function onStart() {
          $theme = CmsClassesTheme::getEditTheme();
          $pageList = new RainLabPagesClassesPageList($theme);
          $treePageList = $pageList->getPageTree(true);
          $pages = ;
          $this->getRecursivePage($pages, $treePageList);
          dd($pages);
          }

          function getRecursivePage(&$pages, $subpages, $level = 0) {
          $level++;
          foreach($subpages as $pageArr) {
          $pages[$pageArr->page->url] =
          str_repeat('-',$level) . ' ' . $pageArr->page->title;
          if(count($pageArr->subpages) > 0) {
          $this->getRecursivePage($pages, $pageArr->subpages, $level);
          }
          }
          }



          Output






          array:9 [▼
          "/content" => "- Content"
          "/content/pages" => "-- Static Pages"
          "/content/content" => "-- Content"
          "/content/models" => "-- Models"
          "/content/urls" => "-- URLs"
          "/content/urls/tesets" => "--- tesets"
          "/test-sp" => "- test-sp"
          "/test-sp/oks" => "-- oks"
          "/test" => "- test"
          ]


          here you can just replace - to &nbsp; or just remove that part [ I added because you had that in code so, may be useful to you. ]




          For your code






          public function boot() {
          RainLabPagesClassesPage::extend(function($model) {
          $model->addDynamicMethod('getPageOptions', function() {
          $theme = CmsClassesTheme::getEditTheme();
          $pageList = new RainLabPagesClassesPageList($theme);
          $treePageList = $pageList->getPageTree(true);
          $pages = ;
          $this->getRecursivePage($pages, $treePageList);
          return $pages;
          });
          });
          }

          public function getRecursivePage(&$pages, $subpages, $level = 0) {
          $level++;
          foreach($subpages as $pageArr) {
          $pages[$pageArr->page->url] =
          str_repeat('-',$level) . ' ' . $pageArr->page->title;
          if(count($pageArr->subpages) > 0) {
          $this->getRecursivePage($pages, $pageArr->subpages, $level);
          }
          }
          }


          If any doubt please comment.






          share|improve this answer


























          • Hi, the code seems does not work after subpages. imgur.com/a/0Tazzmm All the parent pages becomes child, after the subpages loop

            – claudchan
            Nov 27 '18 at 2:31













          • hmm, updated getRecursivePage() method please use new one now it should work.

            – Hardik Satasiya
            Nov 27 '18 at 7:30








          • 1





            Great! It works and thank you!

            – claudchan
            Nov 27 '18 at 9:26














          2












          2








          2







          may be this will help you.





          function onStart() {
          $theme = CmsClassesTheme::getEditTheme();
          $pageList = new RainLabPagesClassesPageList($theme);
          $treePageList = $pageList->getPageTree(true);
          $pages = ;
          $this->getRecursivePage($pages, $treePageList);
          dd($pages);
          }

          function getRecursivePage(&$pages, $subpages, $level = 0) {
          $level++;
          foreach($subpages as $pageArr) {
          $pages[$pageArr->page->url] =
          str_repeat('-',$level) . ' ' . $pageArr->page->title;
          if(count($pageArr->subpages) > 0) {
          $this->getRecursivePage($pages, $pageArr->subpages, $level);
          }
          }
          }



          Output






          array:9 [▼
          "/content" => "- Content"
          "/content/pages" => "-- Static Pages"
          "/content/content" => "-- Content"
          "/content/models" => "-- Models"
          "/content/urls" => "-- URLs"
          "/content/urls/tesets" => "--- tesets"
          "/test-sp" => "- test-sp"
          "/test-sp/oks" => "-- oks"
          "/test" => "- test"
          ]


          here you can just replace - to &nbsp; or just remove that part [ I added because you had that in code so, may be useful to you. ]




          For your code






          public function boot() {
          RainLabPagesClassesPage::extend(function($model) {
          $model->addDynamicMethod('getPageOptions', function() {
          $theme = CmsClassesTheme::getEditTheme();
          $pageList = new RainLabPagesClassesPageList($theme);
          $treePageList = $pageList->getPageTree(true);
          $pages = ;
          $this->getRecursivePage($pages, $treePageList);
          return $pages;
          });
          });
          }

          public function getRecursivePage(&$pages, $subpages, $level = 0) {
          $level++;
          foreach($subpages as $pageArr) {
          $pages[$pageArr->page->url] =
          str_repeat('-',$level) . ' ' . $pageArr->page->title;
          if(count($pageArr->subpages) > 0) {
          $this->getRecursivePage($pages, $pageArr->subpages, $level);
          }
          }
          }


          If any doubt please comment.






          share|improve this answer















          may be this will help you.





          function onStart() {
          $theme = CmsClassesTheme::getEditTheme();
          $pageList = new RainLabPagesClassesPageList($theme);
          $treePageList = $pageList->getPageTree(true);
          $pages = ;
          $this->getRecursivePage($pages, $treePageList);
          dd($pages);
          }

          function getRecursivePage(&$pages, $subpages, $level = 0) {
          $level++;
          foreach($subpages as $pageArr) {
          $pages[$pageArr->page->url] =
          str_repeat('-',$level) . ' ' . $pageArr->page->title;
          if(count($pageArr->subpages) > 0) {
          $this->getRecursivePage($pages, $pageArr->subpages, $level);
          }
          }
          }



          Output






          array:9 [▼
          "/content" => "- Content"
          "/content/pages" => "-- Static Pages"
          "/content/content" => "-- Content"
          "/content/models" => "-- Models"
          "/content/urls" => "-- URLs"
          "/content/urls/tesets" => "--- tesets"
          "/test-sp" => "- test-sp"
          "/test-sp/oks" => "-- oks"
          "/test" => "- test"
          ]


          here you can just replace - to &nbsp; or just remove that part [ I added because you had that in code so, may be useful to you. ]




          For your code






          public function boot() {
          RainLabPagesClassesPage::extend(function($model) {
          $model->addDynamicMethod('getPageOptions', function() {
          $theme = CmsClassesTheme::getEditTheme();
          $pageList = new RainLabPagesClassesPageList($theme);
          $treePageList = $pageList->getPageTree(true);
          $pages = ;
          $this->getRecursivePage($pages, $treePageList);
          return $pages;
          });
          });
          }

          public function getRecursivePage(&$pages, $subpages, $level = 0) {
          $level++;
          foreach($subpages as $pageArr) {
          $pages[$pageArr->page->url] =
          str_repeat('-',$level) . ' ' . $pageArr->page->title;
          if(count($pageArr->subpages) > 0) {
          $this->getRecursivePage($pages, $pageArr->subpages, $level);
          }
          }
          }


          If any doubt please comment.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 27 '18 at 7:29

























          answered Nov 26 '18 at 11:57









          Hardik SatasiyaHardik Satasiya

          4,8921922




          4,8921922













          • Hi, the code seems does not work after subpages. imgur.com/a/0Tazzmm All the parent pages becomes child, after the subpages loop

            – claudchan
            Nov 27 '18 at 2:31













          • hmm, updated getRecursivePage() method please use new one now it should work.

            – Hardik Satasiya
            Nov 27 '18 at 7:30








          • 1





            Great! It works and thank you!

            – claudchan
            Nov 27 '18 at 9:26



















          • Hi, the code seems does not work after subpages. imgur.com/a/0Tazzmm All the parent pages becomes child, after the subpages loop

            – claudchan
            Nov 27 '18 at 2:31













          • hmm, updated getRecursivePage() method please use new one now it should work.

            – Hardik Satasiya
            Nov 27 '18 at 7:30








          • 1





            Great! It works and thank you!

            – claudchan
            Nov 27 '18 at 9:26

















          Hi, the code seems does not work after subpages. imgur.com/a/0Tazzmm All the parent pages becomes child, after the subpages loop

          – claudchan
          Nov 27 '18 at 2:31







          Hi, the code seems does not work after subpages. imgur.com/a/0Tazzmm All the parent pages becomes child, after the subpages loop

          – claudchan
          Nov 27 '18 at 2:31















          hmm, updated getRecursivePage() method please use new one now it should work.

          – Hardik Satasiya
          Nov 27 '18 at 7:30







          hmm, updated getRecursivePage() method please use new one now it should work.

          – Hardik Satasiya
          Nov 27 '18 at 7:30






          1




          1





          Great! It works and thank you!

          – claudchan
          Nov 27 '18 at 9:26





          Great! It works and thank you!

          – claudchan
          Nov 27 '18 at 9:26




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53477068%2fhow-to-get-static-page-dropdown-in-octobercms-with-get-page-tree%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)