繁体   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