对于特定的内容类型,如何更改或删除Drupal或节点插入/保存上的其他模块产生的格式错误。我正在寻找一种可以在模块中使用的方法。
我已经尝试过hook_node_validate(),但是我所能做的就是获取错误并设置错误。

对于特定的内容类型,如何更改或删除Drupal或节点插入/保存上的其他模块产生的格式错误。我正在寻找一种可以在模块中使用的方法。
我已经尝试过hook_node_validate(),但是我所能做的就是获取错误并设置错误。

Answers:
要更改单个字符串的文本,最简单的方法是使用String Overrides模块。那将允许您替换字符串:
“!name字段为必填。”
与(例如):
“字段'!name'是必需的。”
如果要使字段不是必需的,请使用常规的hook_form_alter()实现:
/**
 * Implements hook_form_FORM_ID_alter().
 */
function mymodule_form_node_form_alter(&$form, &$form_state) {
  $node = $form['#node'];
  if ($node->type == 'my_custom_type') {
    $form['title']['#required'] = FALSE;
  }
}
表单具有在$form['#validate']数组中指定的验证功能。表单元素具有在中指定的功能$form['element_key']['#element_validate']。
您可以这样指定自己的:
/**
 * Implements hook_form_FORM_ID_alter().
 */
function mymodule_form_node_form_alter(&$form, &$form_state) {
  $form['title']['#element_validate'][] = 'mymodule_validate_node_title';
}
/**
 * Validate the node title to prevent ALL CAPS.
 */
function mymodule_validate_node_title($element, &$form_state, $form) {
  if (preg_match('/^[A-Z]+$/', $element['#value'])) {
    form_error($element, t('You may not enter titles in ALL CAPS.'));
  }
}
              由于用于该错误消息的字符串是“!name字段为必填”。使用String Overrides模块或更改在settings.php文件中使用的字符串将影响更改用于每个必需表单字段的字符串。
如果要在未输入标题时更改为标题显示的错误字符串,则可以:
hook_form_alter()在这种形式的验证处理程序中:
$form['title']($form使用设置的位置$form = &drupal_static('form_set_error', array());,然后将其更改为所需的字符串'Title field is required.'$_SESSION['messages']['error'](数组)包含字符串'Title field is required.',并将其更改为要显示的字符串避免显示错误更容易:只需将#required属性设置为FALSE,Drupal将不会显示该错误消息。
对于drupal 7,您可以通过zip 下载此模块http://drupal.org/node/1209450,它将为您提供这个钩子。
message_alter(&$messages) {
}
              对于Drupal 8,我能够添加一个自定义的验证功能,该功能可以实际检查现有错误,并根据具体情况更改错误的标记。就我而言,我想更改来自引用用户的entity_autocomplete字段的错误消息。如果添加了无效用户,则验证错误将显示为“没有与%name匹配的实体”。我想用“用户”代替“实体”一词,以减少恐惧并可能使用户感到困惑。
首先,我使用hook_form_alter()添加我的validate函数:
/**
 * Implements hook_form_alter().
 */
function my_module_form_alter(&$form, FormStateInterface $form_state, $form_id) {
      if (in_array($form_id, ['whatever_form_id_you_need_to_alter'])) {
        // Add entity autocomplete custom form validation messages alter.
        array_unshift($form['#validate'], 'my_module_custom_user_validate');
      }
然后,在“ my_module_custom_user_validate”函数中:
/**
 * Custom form validation handler that alters default validation.
 * @param $form
 * @param \Drupal\Core\Form\FormStateInterface $form_state
*/
function my_module_custom_user_validate(&$form, FormStateInterface $form_state) {
      // Check for any errors on the form_state
      $errors = $form_state->getErrors();
      if ($errors) {
        foreach ($errors as $error_key => $error_val) {
          // Check to see if the error is related to the desired field:
          if (strpos($error_key, 'the_entity_reference_field_machine_name') !== FALSE) {
            // Check for the word 'entities', which I want to replace
            if (strpos($error_val->getUntranslatedString(), 'entities') == TRUE) {
              // Get the original args to pass into the new message
              $original_args = $error_val->getArguments();
              // Re-construct the error
              $error_val->__construct("There are no users matching the name %value", $original_args);
            }
          }
        }
      }
    }
希望这可以帮助!