Drupal 7-如何检索对象/ ID并在内部Hook菜单中使用?

问题描述 投票:1回答:1

我正在使用Entity Registration模块,并在其中添加了“取消”标签

function registration_menu() ...

$items['registration/%registration/view'] = array(
    'title' => 'View',
    'page callback' => 'registration_view',
    'page arguments' => array(1),
    'access callback' => 'entity_access',
    'access arguments' => array('view', 'registration', 1),
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );

$items['registration/%registration/cancel'] = array(
'title' => 'Cancel',
'page callback' => 'registration_view_cancel',
'page arguments' => array(1),
'access callback' => 'entity_access',
'access arguments' => array('view', 'registration', 1),
'type' => MENU_LOCAL_TASK,
);

我想根据注册状态通过将选项卡放在if条件内隐藏

我不知道如何加载当前的“注册”对象(ID仅在URL中)以读取状态。我认为这项工作不同于节点类​​型,因为这是一个实体类型“注册”。

如何最好地加载该对象以提取“状态”值?请参阅下面的开发输出

.. (Object) Registration
entityType:protected (String, 12 characters ) registration
entityInfo:protected (Array, 25 elements)
idKey:protected (String, 15 characters ) registration_id
nameKey:protected (String, 15 characters ) registration_id
statusKey:protected (String, 6 characters ) status
defaultLabel:protected (Boolean) FALSE
registration_id (String, 3 characters ) 633
type (String, 9 characters ) classroom
entity_id (String, 4 characters ) 1426
entity_type (String, 4 characters ) node
anon_mail (NULL)
user_uid (String, 4 characters ) 1772
count (String, 1 characters ) 1
author_uid (String, 4 characters ) 1772
state (String, 8 characters ) complete
created (String, 10 characters ) 1591628269
updated (String, 10 characters ) 1591628269

更新dpm($ items);

admin/structure/registration (Array, 6 elements)
registration/%registration (Array, 6 elements)
test/registration2/%registration (Array, 6 elements)
test/registration2/%registration/view (Array, 6 elements)
registration/%registration/view (Array, 6 elements)
registration/%registration/edit (Array, 7 elements)
registration/%registration/delete (Array, 6 elements)
node/%entity_object/register (Array, 7 elements)
node/%entity_object/registrations (Array, 7 elements)
node/%entity_object/registrations/list (Array, 7 elements)
node/%entity_object/registrations/settings (Array, 8 elements)
node/%entity_object/registrations/broadcast (Array, 8 elements)
registration/%registration/devel (Array, 8 elements)
registration/%registration/devel/load (Array, 2 elements)

和注册对象(我所在的页面)

registration/%registration (Array, 6 elements)
title callback (String, 23 characters ) registration_page_title | (Callback) registration_page_title();
title arguments (Array, 1 element)
0 (Integer) 1
page callback (String, 17 characters ) registration_view | (Callback) registration_view();
page arguments (Array, 1 element)
0 (Integer) 1
access callback (String, 13 characters ) entity_access | (Callback) entity_access();
access arguments (Array, 3 elements)
0 (String, 4 characters ) view
1 (String, 12 characters ) registration
2 (Integer) 1
drupal menu drupal-7 hook
1个回答
0
投票

menu_alter将根据在registration_cancel_visibility access callback中设置的标准修改菜单。在这种情况下,如果回调在$ registration-> state中找到“ complete”,它将返回TRUE并显示Cancel按钮,否则将保持隐藏状态。

无需在$items['registration/%registration/cancel']下添加registration_menu

您可能希望更改page callback以将用户定向到取消页面/表单。

将此添加到您的registration.module

/**
* Implements hook_menu_alter.
*/
function registration_menu_alter(&$items) {
    $items['registration/%registration/cancel'] = array(
        'title' => 'Cancel',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('registration_form', 1),
        // access callback to a function to check if the menu item should be visible
        'access callback' => 'registration_cancel_visibility',
        'access arguments' => array('update', 1),
        'weight' => 10,
        'type' => MENU_LOCAL_TASK,
    );
}

/**
*
* Check state of registration and apply visibility of cancel functionality accordingly
*/
function registration_cancel_visibility($action, $registration) {
    // ternary operator to return TRUE or FALSE based on the value of $registration->state
    return $registration->state === "complete" ? TRUE : FALSE;
}
© www.soinside.com 2019 - 2024. All rights reserved.