【问题标题】:How to create custom page having custom links in drupal 7?如何在 drupal 7 中创建具有自定义链接的自定义页面?
【发布时间】:2016-04-25 14:13:55
【问题描述】:

我想使用模块在管理端创建页面。我需要提到使用 hook_menu() 的自定义链接页面。从浏览器访问链接后,我想显示一些链接,以便从另一个网站调用另一个静态链接。

例如:

我想创建管理员/链接列表:自定义 url

在此页面上单击此按钮后,结果将类似于带有按钮的表格列表,用于从另一个网站导航该静态链接。

我已经创建了以下内容。

通过使用以下代码,我创建了带有自定义模板文件分配的自定义页面,通过传递静态链接并将其打印在自定义模板页面中。请注意,我刚刚在模板页面中打印了数组。格式保留。

<?php 
    // Created Custom URL for accesing the static links
    function test_menu() {
        $items['admin/list-of-links'] = array(
            'title' => 'List Section',
            'page callback' => 'list_section',
            'access arguments' => array('administrator'),
        );
    }

    // Created Page Callback for assigning the variable for the theme
    function list_section() {
        $static_links = array("www.google.com", "www.facebook.com");
        return theme('test_link', array('static_links' => $static_links));
    }

    // Assigned the template for the page that we have created 
    function test_theme($existing, $type, $theme, $path) {

        return array(
            'test_link' => array(
                'template' => 'static-link-listing',
                'path' => drupal_get_path('theme', 'seven') . "/templates"
            ),
        );
    }

    //Created Template File :  themes/seven/templates/static-link-listing.tpl.php
    // And after that, I am getting the result.
    // Now after that, we will format what output we need.

    echo "<pre>";
    print_r($static_links);

    ?>

【问题讨论】:

    标签: drupal-7 drupal-modules


    【解决方案1】:

    您将需要使用 hook_menu() 来执行此操作。

    假设您的模块名为示例,那么您需要在 .module 文件中添加以下代码:

    /**
     * Implements hook_menu().
     */
    function example_menu() {
      $items['admin/list-of-links'] = array(
        'description' => 'Load a list lof links',
        'title' => t('List of links'),
        'page callback' => '_example_load_links',
        'access arguments' => array('access content'),
        'type' => MENU_NORMAL_ITEM,
      );
    
      return $items;
    }
    

    要返回所选页面上的链接列表,您需要为页面回调添加一个函数,例如:

    /**
     * We load a list of links
     */
    function _example_load_links(){
      $content['item'] = array(
        '#type' => 'item',
        '#markup' => t('Hello world, place your links here'),
      );
      return $content;
    }
    

    如果您启用模块并清除缓存,这应该可以工作(对于 hook_menu 非常重要)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-15
      • 1970-01-01
      • 2011-07-08
      相关资源
      最近更新 更多