Drupal 7& PHP - 如果$ form是任何内容类型,则显示内容

时间:2013-06-20 20:08:47

标签: php drupal drupal-7

我需要帮助创建一个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);
}

1 个答案:

答案 0 :(得分:1)

如果您在节点/内容中,$form变量将具有节点对象。但是如果表单不是for / from节点那么它将不具有节点对象。您可以这样检查:

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

实际上,我对$form['#node'](:P)有点疑惑。您可以通过调试$form$form_state变量来获取它。