簡體   English   中英

Drupal 7和PHP-如果$ form是任何內容類型,則顯示內容

[英]Drupal 7 & PHP - display content if $form is any Content Type

我需要創建if語句的幫助,該語句僅以節點的形式打印出取消按鈕。 如果沒有if語句,則“取消”按鈕會在所有表單(包括站點搜索表單)上打印出來。 我嘗試使用'$ form_id!=',但是在希望取消按鈕出現的地方添加每個表單ID似乎不是很直觀。 任何幫助將非常感激。

<?php

/**
* Implements hook_form_alter().
*/
function cancel_button_form_alter(&$form, &$form_state, $form_id) {
  // Here is where I'm having trouble. What variable can 
  // I put that targets ANY content type?
  if ($form_id != 'search_block_form') {
    // Add a cancel button.
    $form['actions']['cancel'] = array(
      '#type' => 'submit',
      '#value' => t('Cancel'),
      '#access' => TRUE,
      '#weight' => 15,
      '#submit' => array('cancel_button_form_cancel', 'node_form_submit_build_node'),
      '#limit_validation_errors' => array(),
    );
  }
}

/**
* Custom cancel button callback.
*/
function cancel_button_form_cancel($form, &$form_state) {
$url = $_GET['destination'] ? $_GET['destination'] : '';
drupal_goto($url);
}

如果您在節點/內容中,則$form變量將具有一個節點對象。 但是,如果表單不是針對/來自節點的,則它將沒有節點對象。 您可以像這樣檢查:

if(isset($form['#node'])) {
    // Your code goes here
}

實際上,我對$form['#node'] (:P)有點困惑。 您可以通過調試$form$form_state變量來獲取它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM