【问题标题】:Sorting array in drupal 7drupal 7中的排序数组
【发布时间】:2017-11-22 04:10:55
【问题描述】:

我是 Drupal 的新手。请接受我的无知。我有一个带有自定义钩子的页面,可以覆盖视图并显示默认排序或不排序的帖子列表。我想根据节点创建日期对数组进行排序。但不确定在哪里插入排序参数。有人可以帮忙吗?我在 my_view.module 文件中有这个代码块。

    function _sites_by_park_page_get_node_references($fieldName, $property, $park_id) {
      $results = array();
      $parks = _sites_by_park_page_get_sites($park_id);
      foreach ($parks['features'] as $feature) {
        foreach($feature['properties'][$property] as $key => $value) {
          if (!array_key_exists($key, $results)) {
            $query = new EntityFieldQuery();
            $query->entityCondition('entity_type', 'node');
            $query->fieldCondition($fieldName, 'value', $value);
            $result = $query->execute();
            $node = node_load(array_shift(array_keys($result['node'])));
            $results[$key] = $node->title;
          }
        }
      }
      return $results;
    }

    /**
     * Return GeoJSON formatted park data
     */
    function _sites_by_park_page_get_sites($park_id) {
      // Uses a lot of memory thanks to views and GD image resizing, could use some garbage collection opimisation
      ini_set('memory_limit', '512M');
      $data_view = views_get_view('sites_by_park_data');
      $data_view->set_arguments(array($park_id));
      $data_view->execute('page');

      // Build our own, more lightweight geojson structure for faster page loads (Leaflet module generated 2MB of data vs this custom module's 40kb-ish for 140 parks)
      $results = array(
        'type' => 'FeatureCollection',
        'features' => array(),
      );
      foreach($data_view->result as $result) {
        // Initialise some arrays that may be merged between result sets (for instance when multiple activities contribute to a site's overall data)
        if (!isset($results['features'][$result->nid])) {
          $results['features'][$result->nid] = array(
            'type' => 'Feature',
            'properties' => array()
          );
        }



  }   
  $results['features'] = array_values($results['features']);
  return $results;
}

【问题讨论】:

  • 你为什么不从视图的管理页面设置正常的订购方式?为什么你必须从代码中做到这一点?
  • 这是一个很好的提示。出于某种原因,我没有考虑到这一点。谢谢米兰G
  • 您可以从后端执行的所有操作 - 从后端执行。这就是使用 CMS 的意义所在。 :)

标签: arrays sorting drupal drupal-7


【解决方案1】:

根据 MilanG 的建议,我已通过管理部分完成此操作。更改排序顺序,现在可以正常工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    相关资源
    最近更新 更多