serial.fields.inc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * @file
  4. * Serial (Fields Helper).
  5. */
  6. /**
  7. * Class SerialFields.
  8. */
  9. class SerialFields {
  10. private $fields = array();
  11. private $instances = array();
  12. /**
  13. * Set field definitions.
  14. *
  15. * @param array[] $fields
  16. * An associative array where keys - field names and values - definition.
  17. */
  18. public function __construct(array $fields) {
  19. $this->fields = $fields;
  20. }
  21. /**
  22. * Create fields.
  23. *
  24. * @return self
  25. * Object instance.
  26. *
  27. * @throws \FieldException
  28. * When cannot create a field.
  29. */
  30. public function create() {
  31. foreach ($this->fields as $name => $data) {
  32. if (!db_table_exists("field_data_$name")) {
  33. field_create_field($data + array(
  34. 'default' => '',
  35. 'not null' => TRUE,
  36. 'field_name' => $name,
  37. ));
  38. }
  39. }
  40. return $this;
  41. }
  42. /**
  43. * Completely delete fields.
  44. *
  45. * This function deletes tables: "field_data_NAME" and "field_revision_NAME"
  46. * and entries in "field_config" and "field_config_instances".
  47. *
  48. * @return self
  49. * Object instance.
  50. */
  51. public function delete() {
  52. foreach (array_keys($this->fields) as $name) {
  53. // Delete tables.
  54. foreach (array('data', 'revision') as $table_type) {
  55. $table = "field_{$table_type}_{$name}";
  56. if (db_table_exists($table)) {
  57. db_drop_table($table);
  58. }
  59. }
  60. // Delete entries.
  61. foreach (array('config', 'config_instance') as $table_type) {
  62. db_delete("field_$table_type")
  63. ->condition('field_name', $name)
  64. ->execute();
  65. }
  66. }
  67. return $this;
  68. }
  69. /**
  70. * Attach existing fields into entity.
  71. *
  72. * @param string $entity_type
  73. * Entity machine name.
  74. * @param string $bundle_name
  75. * Entity bundle name.
  76. *
  77. * @return self
  78. * Object instance.
  79. *
  80. * @throws \FieldException
  81. * When instance cannot be created.
  82. */
  83. public function attach($entity_type, $bundle_name) {
  84. $attached_fields = field_info_instances($entity_type, $bundle_name);
  85. foreach ($this->fields as $field_name => $data) {
  86. if (empty($attached_fields[$field_name]) && field_info_field($field_name)) {
  87. // Provide a possibility to specify field weight, depending on
  88. // another one.
  89. //
  90. // @code
  91. // $fields = array(
  92. // 'field_title' => array(
  93. // 'type' => 'text',
  94. // 'label' => 'Title',
  95. // 'widget' => array(
  96. // 'weight' => 10,
  97. // ),
  98. // ),
  99. // 'field_description' => array(
  100. // 'type' => 'text',
  101. // 'label' => 'Description',
  102. // 'widget' => array(
  103. // // Weight of this field will be "9".
  104. // 'weight' => array('field_title', -1),
  105. // ),
  106. // ),
  107. // );
  108. // @endcode
  109. if (isset($data['widget']['weight']) && is_array($data['widget']['weight'])) {
  110. list($dependent, $calc) = $data['widget']['weight'];
  111. $dependent = field_info_instance($entity_type, $dependent, $bundle_name);
  112. if (!empty($dependent)) {
  113. $data['widget']['weight'] = $dependent['widget']['weight'] + $calc;
  114. }
  115. }
  116. field_create_instance($data + array(
  117. 'bundle' => $bundle_name,
  118. 'field_name' => $field_name,
  119. 'entity_type' => $entity_type,
  120. ));
  121. }
  122. }
  123. return $this;
  124. }
  125. /**
  126. * Get field instances.
  127. *
  128. * @return array[]
  129. * Field instances.
  130. */
  131. public function &getInstances() {
  132. if (empty($this->instances)) {
  133. $query = db_select('field_config_instance', 'fci')
  134. ->fields('fci', array('field_name', 'data'))
  135. ->condition('field_name', array_keys($this->fields))
  136. ->execute()
  137. ->fetchAllKeyed();
  138. $this->instances = array_map('unserialize', $query);
  139. }
  140. return $this->instances;
  141. }
  142. /**
  143. * Field definitions getter.
  144. *
  145. * @return array[]
  146. * Field definitions.
  147. */
  148. public function getFields() {
  149. return $this->fields;
  150. }
  151. /**
  152. * Save field instances.
  153. *
  154. * @return self
  155. * Object instance.
  156. *
  157. * @throws \Exception
  158. * @throws \InvalidMergeQueryException
  159. */
  160. public function saveInstances() {
  161. foreach ($this->instances as $field_name => $data) {
  162. db_merge('field_config_instance')
  163. ->fields(array('data' => serialize($data)))
  164. ->condition('field_name', $field_name)
  165. ->execute();
  166. }
  167. return $this;
  168. }
  169. }