FieldInstance.inc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. class FieldInstance {
  3. /**
  4. * @var string
  5. * The instance's entity type.
  6. */
  7. public $entityType;
  8. /**
  9. * @var string
  10. * The instance bundle.
  11. */
  12. public $bundle;
  13. /**
  14. * @var string
  15. * The field's machine name.
  16. */
  17. public $name;
  18. /**
  19. * @var boolean
  20. * Whether or not this instance's entity type supports bundles.
  21. */
  22. public $isBundleable;
  23. /**
  24. * @var string
  25. * The human-readable label of the instance's entity type.
  26. */
  27. public $entityTypeLabel;
  28. /**
  29. * @var string
  30. * The human-readable label of the instance's bundle.
  31. */
  32. public $bundleLabel;
  33. /**
  34. * @var integer
  35. * The cardinality (maximum values) the field supports, or
  36. * FIELD_CARDINALITY_UNLIMITED.
  37. */
  38. public $cardinality;
  39. /**
  40. * @var string
  41. * The instance's label.
  42. */
  43. public $label;
  44. /**
  45. * @var FieldInstance
  46. * The parent of this instance, if any.
  47. */
  48. protected $parent;
  49. /**
  50. * @var FieldInstance
  51. * The child of this instance, if any.
  52. */
  53. protected $child;
  54. public function __construct($entity_type, $bundle, $field_name) {
  55. $this->entityType = $entity_type;
  56. $this->bundle = $bundle;
  57. $this->name = $field_name;
  58. // Get info about the entity type and bundle hosting this field instance.
  59. $info = entity_get_info($entity_type);
  60. $this->isBundleable = (boolean) $info['entity keys']['bundle'];
  61. $this->entityTypeLabel = $info['label'];
  62. $this->bundleLabel = $info['bundles'][$bundle]['label'];
  63. // Get global info about the field.
  64. $info = field_info_field($field_name);
  65. $this->cardinality = $info['cardinality'];
  66. // Finally, get info about the field instance.
  67. $instance = field_info_instance($entity_type, $field_name, $bundle);
  68. $this->label = $instance['label'];
  69. }
  70. public function __toString() {
  71. return "{$this->entityType}:{$this->bundle}:{$this->name}";
  72. }
  73. /**
  74. * Get or set the parent of this field instance.
  75. */
  76. public function parent(FieldInstance $parent = NULL) {
  77. if ($parent) {
  78. $this->parent = $parent;
  79. }
  80. return $this->parent;
  81. }
  82. /**
  83. * Get or set the child of this field instance.
  84. */
  85. public function child(FieldInstance $child = NULL) {
  86. if ($child) {
  87. $this->child = $child;
  88. }
  89. return $this->child;
  90. }
  91. /**
  92. * Determine if this field requires a parent. An example of this would be
  93. * a field that is instantiated on a field collection (which is itself
  94. * a field).
  95. */
  96. public function requireParent() {
  97. return FALSE;
  98. }
  99. /**
  100. * Return the parents of this field instance as an array of FieldInstance
  101. * objects. If there are no parents, return an empty array.
  102. */
  103. public function getParents() {
  104. return array();
  105. }
  106. }