ExpressionEngine:如何获取给定其entry_id的页面路径(带有结构插件)

时间:2011-11-23 16:09:27

标签: php structure expressionengine

我正在尝试构建一个扩展程序来创建自动重定向短网址的页面,并简化它,我需要在给定其entry_id的情况下获取页面的路径。

说,我有一个包含路径的页面:http://server.tld/index.php/path/to/my/page 但是,在代码中,我只知道此页面的entry_id。

如果我查看exp_channel_titles表,我可以获取url_title字段。但它只包含“页面”。我想得到“/ path / to / my / page”。并且似乎没有任何API。

你知道我该怎么办吗?

非常感谢。

3 个答案:

答案 0 :(得分:4)

我不记得文档中的确切位置,但我认为您的问题来自于直接从数据库中检索页面uris这一事实。

它们位于Expressionengine全局配置变量中。我已经能够使用以下代码使用entry_id进行URL查找:

注意:这假设您使用的是结构,页面模块等。

<?php 

   $this->EE =& get_instance(); // Get global configuration variable

   $site_id = $this->EE->config->item('site_id'); // Get site id (MSM safety)
   $site_pages = $this->EE->config->item('site_pages'); // Get pages array

   /**  The array is indexed as follows:
    *   $site_pages[{site_id}]['url'] = '{site_url}
    *   $site_pages[{site_id}]['uris']['entry_id'] = {page_uri}
   **/

   $page_url = $site_pages[$site_id]['uris'][$entry_id];

?>

编辑:  我最初声明uris不在数据库中,严格来说并不是真的...页面实际上存储为exp_sites.site_pages的网站字符串索引的散列字符串。

答案 1 :(得分:0)

我没有找到比以下代码更好的东西:

  //
  //
  // Don't look the following code just yet . . .
  //
  //
  // You'll be pulling your hair out of your head. Just read me first.

  // Following is a huge SQL query, that assume that pages are not nested
  // than 9 times. This is actually a hard limitation of EE, and I'm using
  // that to get the information I need in only one query instead of nine.
  //
  // The following code is just to get the path of the current entry we are
  // editing, so the redirect page will know where to redirect. And I did
  // not find any function or API to get this information. Too bad.
  //
  // If you find any solution to that, please answer
  // http://stackoverflow.com/questions/8245405/expressionengine-how-to-get-the-path-of-a-page-given-its-entry-id-with-the-str
  // it might save other people all the trouble.

  // S
  //
  // P
  //
  // O
  //
  // I
  //
  // L
  //
  // E
  //
  // R

  // First, we get all the entry_id of all the elements that are parent to
  // the current element in the structure table (created by the Structure
  // plugin).

  $q = $this->EE->db->query(
    "SELECT
      S1.entry_id AS entry9,
      S2.entry_id AS entry8,
      S3.entry_id AS entry7,
      S4.entry_id AS entry6,
      S5.entry_id AS entry5,
      S3.entry_id AS entry4,
      S7.entry_id AS entry3,
      S8.entry_id AS entry2,
      S9.entry_id AS entry1
    FROM
      exp_structure AS S1,
      exp_structure AS S2,
      exp_structure AS S3,
      exp_structure AS S4,
      exp_structure AS S5,
      exp_structure AS S6,
      exp_structure AS S7,
      exp_structure AS S8,
      exp_structure AS S9
    WHERE
      S1.entry_id = $entry_id AND
      S1.parent_id = S2.entry_id AND
      S2.parent_id = S3.entry_id AND
      S3.parent_id = S4.entry_id AND
      S4.parent_id = S5.entry_id AND
      S5.parent_id = S6.entry_id AND
      S6.parent_id = S7.entry_id AND
      S7.parent_id = S8.entry_id AND
      S8.parent_id = S9.entry_id");

  // Then, we construct a similar query to get all the url_title attributes
  // for these pages.

  $path = array();
  $sql = array("SELECT" => "SELECT", "FROM" => " FROM", "WHERE" => " WHERE");
  $j = 1;
  for($i = 1; $i <= 9; ++$i){
    $id = $q->row("entry$i");
    if($id > 0){
      $sql['SELECT'] .= " CT$j.url_title AS title$j,";
      $sql['FROM']   .= " exp_channel_titles as CT$j,";
      $sql['WHERE']  .= " CT$j.entry_id = $id AND";
      $j++;
    }
  }
  $sql['SELECT'] = rtrim($sql['SELECT'], ",");
  $sql['FROM']   = rtrim($sql['FROM'], ",");
  $sql['WHERE'] = preg_replace("/ AND$/", "", $sql['WHERE']);
  $sql = $sql['SELECT'] . $sql['FROM'] . $sql['WHERE'];
  $q = $this->EE->db->query($sql);

  // Finally, we can construct the path for the current page

  $path = "/";
  for($i = 1; $i < $j; ++$i){
    $path .= $q->row("title$i") . '/';
  }

  //
  // Blood and bloody ashes.
  //

答案 2 :(得分:0)

我可以建议通过他们的support forum来询问结构开发者吗?