如何检查实体中是否存在字段?


28

在实体节点中,我要检查“ abc”字段是否存在。如果存在,我将通过$value->get('abc').

但是该字段不存在的内容类型给出错误:

该网站遇到意外错误。请稍后再试。

如何检查实体中是否存在字段?

Answers:


57

您可以使用以下方法hasField()

$entity->hasField('abc')

6
万一别人得到由该抓出来,hasField只能在Drupal 8
Monkeybrain
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.