EntityReference_SelectionHandler_Generic.class.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. <?php
  2. /**
  3. * A generic Entity handler.
  4. *
  5. * The generic base implementation has a variety of overrides to workaround
  6. * core's largely deficient entity handling.
  7. */
  8. class EntityReference_SelectionHandler_Generic implements EntityReference_SelectionHandler {
  9. /**
  10. * Implements EntityReferenceHandler::getInstance().
  11. */
  12. public static function getInstance($field, $instance = NULL, $entity_type = NULL, $entity = NULL) {
  13. $target_entity_type = $field['settings']['target_type'];
  14. // Check if the entity type does exist and has a base table.
  15. $entity_info = entity_get_info($target_entity_type);
  16. if (empty($entity_info['base table'])) {
  17. return EntityReference_SelectionHandler_Broken::getInstance($field, $instance);
  18. }
  19. if (class_exists($class_name = 'EntityReference_SelectionHandler_Generic_' . $target_entity_type)) {
  20. return new $class_name($field, $instance, $entity_type, $entity);
  21. }
  22. else {
  23. return new EntityReference_SelectionHandler_Generic($field, $instance, $entity_type, $entity);
  24. }
  25. }
  26. protected function __construct($field, $instance = NULL, $entity_type = NULL, $entity = NULL) {
  27. $this->field = $field;
  28. $this->instance = $instance;
  29. $this->entity_type = $entity_type;
  30. $this->entity = $entity;
  31. }
  32. /**
  33. * Implements EntityReferenceHandler::settingsForm().
  34. */
  35. public static function settingsForm($field, $instance) {
  36. $entity_info = entity_get_info($field['settings']['target_type']);
  37. // Merge-in default values.
  38. $field['settings']['handler_settings'] += array(
  39. 'target_bundles' => array(),
  40. 'sort' => array(
  41. 'type' => 'none',
  42. )
  43. );
  44. if (!empty($entity_info['entity keys']['bundle'])) {
  45. $bundles = array();
  46. foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
  47. $bundles[$bundle_name] = $bundle_info['label'];
  48. }
  49. $form['target_bundles'] = array(
  50. '#type' => 'checkboxes',
  51. '#title' => t('Target bundles'),
  52. '#options' => $bundles,
  53. '#default_value' => $field['settings']['handler_settings']['target_bundles'],
  54. '#size' => 6,
  55. '#multiple' => TRUE,
  56. '#description' => t('The bundles of the entity type that can be referenced. Optional, leave empty for all bundles.'),
  57. '#element_validate' => array('_entityreference_element_validate_filter'),
  58. );
  59. }
  60. else {
  61. $form['target_bundles'] = array(
  62. '#type' => 'value',
  63. '#value' => array(),
  64. );
  65. }
  66. $form['sort']['type'] = array(
  67. '#type' => 'select',
  68. '#title' => t('Sort by'),
  69. '#options' => array(
  70. 'none' => t("Don't sort"),
  71. 'property' => t('A property of the base table of the entity'),
  72. 'field' => t('A field attached to this entity'),
  73. ),
  74. '#ajax' => TRUE,
  75. '#limit_validation_errors' => array(),
  76. '#default_value' => $field['settings']['handler_settings']['sort']['type'],
  77. );
  78. $form['sort']['settings'] = array(
  79. '#type' => 'container',
  80. '#attributes' => array('class' => array('entityreference-settings')),
  81. '#process' => array('_entityreference_form_process_merge_parent'),
  82. );
  83. if ($field['settings']['handler_settings']['sort']['type'] == 'property') {
  84. // Merge-in default values.
  85. $field['settings']['handler_settings']['sort'] += array(
  86. 'property' => NULL,
  87. );
  88. $form['sort']['settings']['property'] = array(
  89. '#type' => 'select',
  90. '#title' => t('Sort property'),
  91. '#required' => TRUE,
  92. '#options' => drupal_map_assoc($entity_info['schema_fields_sql']['base table']),
  93. '#default_value' => $field['settings']['handler_settings']['sort']['property'],
  94. );
  95. }
  96. elseif ($field['settings']['handler_settings']['sort']['type'] == 'field') {
  97. // Merge-in default values.
  98. $field['settings']['handler_settings']['sort'] += array(
  99. 'field' => NULL,
  100. );
  101. $fields = array();
  102. foreach (field_info_instances($field['settings']['target_type']) as $bundle_name => $bundle_instances) {
  103. foreach ($bundle_instances as $instance_name => $instance_info) {
  104. $field_info = field_info_field($instance_name);
  105. foreach ($field_info['columns'] as $column_name => $column_info) {
  106. $fields[$instance_name . ':' . $column_name] = t('@label (column @column)', array('@label' => $instance_info['label'], '@column' => $column_name));
  107. }
  108. }
  109. }
  110. $form['sort']['settings']['field'] = array(
  111. '#type' => 'select',
  112. '#title' => t('Sort field'),
  113. '#required' => TRUE,
  114. '#options' => $fields,
  115. '#default_value' => $field['settings']['handler_settings']['sort']['field'],
  116. );
  117. }
  118. if ($field['settings']['handler_settings']['sort']['type'] != 'none') {
  119. // Merge-in default values.
  120. $field['settings']['handler_settings']['sort'] += array(
  121. 'direction' => 'ASC',
  122. );
  123. $form['sort']['settings']['direction'] = array(
  124. '#type' => 'select',
  125. '#title' => t('Sort direction'),
  126. '#required' => TRUE,
  127. '#options' => array(
  128. 'ASC' => t('Ascending'),
  129. 'DESC' => t('Descending'),
  130. ),
  131. '#default_value' => $field['settings']['handler_settings']['sort']['direction'],
  132. );
  133. }
  134. return $form;
  135. }
  136. /**
  137. * Implements EntityReferenceHandler::getReferencableEntities().
  138. */
  139. public function getReferencableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
  140. $options = array();
  141. $entity_type = $this->field['settings']['target_type'];
  142. $query = $this->buildEntityFieldQuery($match, $match_operator);
  143. if ($limit > 0) {
  144. $query->range(0, $limit);
  145. }
  146. $results = $query->execute();
  147. if (!empty($results[$entity_type])) {
  148. $entities = entity_load($entity_type, array_keys($results[$entity_type]));
  149. foreach ($entities as $entity_id => $entity) {
  150. list(,, $bundle) = entity_extract_ids($entity_type, $entity);
  151. $options[$bundle][$entity_id] = check_plain($this->getLabel($entity));
  152. }
  153. }
  154. return $options;
  155. }
  156. /**
  157. * Implements EntityReferenceHandler::countReferencableEntities().
  158. */
  159. public function countReferencableEntities($match = NULL, $match_operator = 'CONTAINS') {
  160. $query = $this->buildEntityFieldQuery($match, $match_operator);
  161. return $query
  162. ->count()
  163. ->execute();
  164. }
  165. /**
  166. * Implements EntityReferenceHandler::validateReferencableEntities().
  167. */
  168. public function validateReferencableEntities(array $ids) {
  169. if ($ids) {
  170. $entity_type = $this->field['settings']['target_type'];
  171. $query = $this->buildEntityFieldQuery();
  172. $query->entityCondition('entity_id', $ids, 'IN');
  173. $result = $query->execute();
  174. if (!empty($result[$entity_type])) {
  175. return array_keys($result[$entity_type]);
  176. }
  177. }
  178. return array();
  179. }
  180. /**
  181. * Implements EntityReferenceHandler::validateAutocompleteInput().
  182. */
  183. public function validateAutocompleteInput($input, &$element, &$form_state, $form) {
  184. $entities = $this->getReferencableEntities($input, '=', 6);
  185. if (empty($entities)) {
  186. // Error if there are no entities available for a required field.
  187. form_error($element, t('There are no entities matching "%value"', array('%value' => $input)));
  188. }
  189. elseif (count($entities) > 5) {
  190. // Error if there are more than 5 matching entities.
  191. form_error($element, t('Many entities are called %value. Specify the one you want by appending the id in parentheses, like "@value (@id)"', array(
  192. '%value' => $input,
  193. '@value' => $input,
  194. '@id' => key($entities),
  195. )));
  196. }
  197. elseif (count($entities) > 1) {
  198. // More helpful error if there are only a few matching entities.
  199. $multiples = array();
  200. foreach ($entities as $id => $name) {
  201. $multiples[] = $name . ' (' . $id . ')';
  202. }
  203. form_error($element, t('Multiple entities match this reference; "%multiple"', array('%multiple' => implode('", "', $multiples))));
  204. }
  205. else {
  206. // Take the one and only matching entity.
  207. return key($entities);
  208. }
  209. }
  210. /**
  211. * Build an EntityFieldQuery to get referencable entities.
  212. */
  213. protected function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS') {
  214. $query = new EntityFieldQuery();
  215. $query->entityCondition('entity_type', $this->field['settings']['target_type']);
  216. if (!empty($this->field['settings']['handler_settings']['target_bundles'])) {
  217. $query->entityCondition('bundle', $this->field['settings']['handler_settings']['target_bundles'], 'IN');
  218. }
  219. if (isset($match)) {
  220. $entity_info = entity_get_info($this->field['settings']['target_type']);
  221. if (isset($entity_info['entity keys']['label'])) {
  222. $query->propertyCondition($entity_info['entity keys']['label'], $match, $match_operator);
  223. }
  224. }
  225. // Add a generic entity access tag to the query.
  226. $query->addTag($this->field['settings']['target_type'] . '_access');
  227. $query->addTag('entityreference');
  228. $query->addMetaData('field', $this->field);
  229. $query->addMetaData('entityreference_selection_handler', $this);
  230. // Add the sort option.
  231. if (!empty($this->field['settings']['handler_settings']['sort'])) {
  232. $sort_settings = $this->field['settings']['handler_settings']['sort'];
  233. if ($sort_settings['type'] == 'property') {
  234. $query->propertyOrderBy($sort_settings['property'], $sort_settings['direction']);
  235. }
  236. elseif ($sort_settings['type'] == 'field') {
  237. list($field, $column) = explode(':', $sort_settings['field'], 2);
  238. $query->fieldOrderBy($field, $column, $sort_settings['direction']);
  239. }
  240. }
  241. return $query;
  242. }
  243. /**
  244. * Implements EntityReferenceHandler::entityFieldQueryAlter().
  245. */
  246. public function entityFieldQueryAlter(SelectQueryInterface $query) {
  247. }
  248. /**
  249. * Helper method: pass a query to the alteration system again.
  250. *
  251. * This allow Entity Reference to add a tag to an existing query, to ask
  252. * access control mechanisms to alter it again.
  253. */
  254. protected function reAlterQuery(SelectQueryInterface $query, $tag, $base_table) {
  255. // Save the old tags and metadata.
  256. // For some reason, those are public.
  257. $old_tags = $query->alterTags;
  258. $old_metadata = $query->alterMetaData;
  259. $query->alterTags = array($tag => TRUE);
  260. $query->alterMetaData['base_table'] = $base_table;
  261. drupal_alter(array('query', 'query_' . $tag), $query);
  262. // Restore the tags and metadata.
  263. $query->alterTags = $old_tags;
  264. $query->alterMetaData = $old_metadata;
  265. }
  266. /**
  267. * Implements EntityReferenceHandler::getLabel().
  268. */
  269. public function getLabel($entity) {
  270. return entity_label($this->field['settings']['target_type'], $entity);
  271. }
  272. /**
  273. * Ensure a base table exists for the query.
  274. *
  275. * If we have a field-only query, we want to assure we have a base-table
  276. * so we can later alter the query in entityFieldQueryAlter().
  277. *
  278. * @param $query
  279. * The Select query.
  280. *
  281. * @return
  282. * The alias of the base-table.
  283. */
  284. public function ensureBaseTable(SelectQueryInterface $query) {
  285. $tables = $query->getTables();
  286. // Check the current base table.
  287. foreach ($tables as $table) {
  288. if (empty($table['join'])) {
  289. $alias = $table['alias'];
  290. break;
  291. }
  292. }
  293. if (strpos($alias, 'field_data_') !== 0) {
  294. // The existing base-table is the correct one.
  295. return $alias;
  296. }
  297. // Join the known base-table.
  298. $target_type = $this->field['settings']['target_type'];
  299. $entity_info = entity_get_info($target_type);
  300. $id = $entity_info['entity keys']['id'];
  301. // Return the alias of the table.
  302. return $query->innerJoin($target_type, NULL, "$target_type.$id = $alias.entity_id");
  303. }
  304. }
  305. /**
  306. * Override for the Node type.
  307. *
  308. * This only exists to workaround core bugs.
  309. */
  310. class EntityReference_SelectionHandler_Generic_node extends EntityReference_SelectionHandler_Generic {
  311. public function entityFieldQueryAlter(SelectQueryInterface $query) {
  312. // Adding the 'node_access' tag is sadly insufficient for nodes: core
  313. // requires us to also know about the concept of 'published' and
  314. // 'unpublished'. We need to do that as long as there are no access control
  315. // modules in use on the site. As long as one access control module is there,
  316. // it is supposed to handle this check.
  317. if (!user_access('bypass node access') && !count(module_implements('node_grants'))) {
  318. $base_table = $this->ensureBaseTable($query);
  319. $query->condition("$base_table.status", NODE_PUBLISHED);
  320. }
  321. }
  322. }
  323. /**
  324. * Override for the User type.
  325. *
  326. * This only exists to workaround core bugs.
  327. */
  328. class EntityReference_SelectionHandler_Generic_user extends EntityReference_SelectionHandler_Generic {
  329. /**
  330. * Implements EntityReferenceHandler::settingsForm().
  331. */
  332. public static function settingsForm($field, $instance) {
  333. $settings = $field['settings']['handler_settings'];
  334. $form = parent::settingsForm($field, $instance);
  335. $form['referenceable_roles'] = array(
  336. '#type' => 'checkboxes',
  337. '#title' => t('User roles that can be referenced'),
  338. '#default_value' => isset($settings['referenceable_roles']) ? array_filter($settings['referenceable_roles']) : array(),
  339. '#options' => user_roles(TRUE),
  340. );
  341. $form['referenceable_status'] = array(
  342. '#type' => 'checkboxes',
  343. '#title' => t('User status that can be referenced'),
  344. '#default_value' => isset($settings['referenceable_status']) ? array_filter($settings['referenceable_status']) : array('active' => 'active'),
  345. '#options' => array('active' => t('Active'), 'blocked' => t('Blocked')),
  346. );
  347. return $form;
  348. }
  349. public function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS') {
  350. $query = parent::buildEntityFieldQuery($match, $match_operator);
  351. // The user entity doesn't have a label column.
  352. if (isset($match)) {
  353. $query->propertyCondition('name', $match, $match_operator);
  354. }
  355. $field = $this->field;
  356. $settings = $field['settings']['handler_settings'];
  357. $referenceable_roles = isset($settings['referenceable_roles']) ? array_filter($settings['referenceable_roles']) : array();
  358. $referenceable_status = isset($settings['referenceable_status']) ? array_filter($settings['referenceable_status']) : array('active' => 'active');
  359. // If this filter is not filled, use the users access permissions.
  360. if (empty($referenceable_status)) {
  361. // Adding the 'user_access' tag is sadly insufficient for users: core
  362. // requires us to also know about the concept of 'blocked' and 'active'.
  363. if (!user_access('administer users')) {
  364. $query->propertyCondition('status', 1);
  365. }
  366. }
  367. elseif (count($referenceable_status) == 1) {
  368. $values = array('active' => 1, 'blocked' => 0);
  369. $query->propertyCondition('status', $values[key($referenceable_status)]);
  370. }
  371. return $query;
  372. }
  373. public function entityFieldQueryAlter(SelectQueryInterface $query) {
  374. $conditions = &$query->conditions();
  375. if (user_access('administer users')) {
  376. // If the user is administrator, we need to make sure to
  377. // match the anonymous user, that doesn't actually have a name in the
  378. // database.
  379. foreach ($conditions as $key => $condition) {
  380. if ($key !== '#conjunction' && is_string($condition['field']) && $condition['field'] === 'users.name') {
  381. // Remove the condition.
  382. unset($conditions[$key]);
  383. // Re-add the condition and a condition on uid = 0 so that we end up
  384. // with a query in the form:
  385. // WHERE (name LIKE :name) OR (:anonymous_name LIKE :name AND uid = 0)
  386. $or = db_or();
  387. $or->condition($condition['field'], $condition['value'], $condition['operator']);
  388. // Sadly, the Database layer doesn't allow us to build a condition
  389. // in the form ':placeholder = :placeholder2', because the 'field'
  390. // part of a condition is always escaped.
  391. // As a (cheap) workaround, we separately build a condition with no
  392. // field, and concatenate the field and the condition separately.
  393. $value_part = db_and();
  394. $value_part->condition('anonymous_name', $condition['value'], $condition['operator']);
  395. $value_part->compile(Database::getConnection(), $query);
  396. $or->condition(db_and()
  397. ->where(str_replace('anonymous_name', ':anonymous_name', (string) $value_part), $value_part->arguments() + array(':anonymous_name' => format_username(user_load(0))))
  398. ->condition('users.uid', 0)
  399. );
  400. $query->condition($or);
  401. }
  402. }
  403. }
  404. $field = $this->field;
  405. $settings = $field['settings']['handler_settings'];
  406. $referenceable_roles = isset($settings['referenceable_roles']) ? array_filter($settings['referenceable_roles']) : array();
  407. if (!$referenceable_roles || !empty($referenceable_roles[DRUPAL_AUTHENTICATED_RID])) {
  408. // Return early if "authenticated user" choosen.
  409. return;
  410. }
  411. if (!isset($referenceable_roles[DRUPAL_AUTHENTICATED_RID])) {
  412. $query->join('users_roles', 'users_roles', 'users.uid = users_roles.uid');
  413. $query->condition('users_roles.rid', array_keys($referenceable_roles), 'IN');
  414. }
  415. }
  416. }
  417. /**
  418. * Override for the Comment type.
  419. *
  420. * This only exists to workaround core bugs.
  421. */
  422. class EntityReference_SelectionHandler_Generic_comment extends EntityReference_SelectionHandler_Generic {
  423. public function entityFieldQueryAlter(SelectQueryInterface $query) {
  424. // Adding the 'comment_access' tag is sadly insufficient for comments: core
  425. // requires us to also know about the concept of 'published' and
  426. // 'unpublished'.
  427. if (!user_access('administer comments')) {
  428. $base_table = $this->ensureBaseTable($query);
  429. $query->condition("$base_table.status", COMMENT_PUBLISHED);
  430. }
  431. // The Comment module doesn't implement any proper comment access,
  432. // and as a consequence doesn't make sure that comments cannot be viewed
  433. // when the user doesn't have access to the node.
  434. $tables = $query->getTables();
  435. $base_table = key($tables);
  436. $node_alias = $query->innerJoin('node', 'n', '%alias.nid = ' . $base_table . '.nid');
  437. // Pass the query to the node access control.
  438. $this->reAlterQuery($query, 'node_access', $node_alias);
  439. // Alas, the comment entity exposes a bundle, but doesn't have a bundle column
  440. // in the database. We have to alter the query ourself to go fetch the
  441. // bundle.
  442. $conditions = &$query->conditions();
  443. foreach ($conditions as $key => &$condition) {
  444. if ($key !== '#conjunction' && is_string($condition['field']) && $condition['field'] === 'node_type') {
  445. $condition['field'] = $node_alias . '.type';
  446. foreach ($condition['value'] as &$value) {
  447. if (substr($value, 0, 13) == 'comment_node_') {
  448. $value = substr($value, 13);
  449. }
  450. }
  451. break;
  452. }
  453. }
  454. // Passing the query to node_query_node_access_alter() is sadly
  455. // insufficient for nodes.
  456. // @see EntityReferenceHandler_node::entityFieldQueryAlter()
  457. if (!user_access('bypass node access') && !count(module_implements('node_grants'))) {
  458. $query->condition($node_alias . '.status', 1);
  459. }
  460. }
  461. }
  462. /**
  463. * Override for the File type.
  464. *
  465. * This only exists to workaround core bugs.
  466. */
  467. class EntityReference_SelectionHandler_Generic_file extends EntityReference_SelectionHandler_Generic {
  468. public function entityFieldQueryAlter(SelectQueryInterface $query) {
  469. // Core forces us to know about 'permanent' vs. 'temporary' files.
  470. $tables = $query->getTables();
  471. $base_table = key($tables);
  472. $query->condition('status', FILE_STATUS_PERMANENT);
  473. // Access control to files is a very difficult business. For now, we are not
  474. // going to give it a shot.
  475. // @todo: fix this when core access control is less insane.
  476. return $query;
  477. }
  478. public function getLabel($entity) {
  479. // The file entity doesn't have a label. More over, the filename is
  480. // sometimes empty, so use the basename in that case.
  481. return $entity->filename !== '' ? $entity->filename : basename($entity->uri);
  482. }
  483. }
  484. /**
  485. * Override for the Taxonomy term type.
  486. *
  487. * This only exists to workaround core bugs.
  488. */
  489. class EntityReference_SelectionHandler_Generic_taxonomy_term extends EntityReference_SelectionHandler_Generic {
  490. public function entityFieldQueryAlter(SelectQueryInterface $query) {
  491. // The Taxonomy module doesn't implement any proper taxonomy term access,
  492. // and as a consequence doesn't make sure that taxonomy terms cannot be viewed
  493. // when the user doesn't have access to the vocabulary.
  494. $base_table = $this->ensureBaseTable($query);
  495. $vocabulary_alias = $query->innerJoin('taxonomy_vocabulary', 'n', '%alias.vid = ' . $base_table . '.vid');
  496. $query->addMetadata('base_table', $vocabulary_alias);
  497. // Pass the query to the taxonomy access control.
  498. $this->reAlterQuery($query, 'taxonomy_vocabulary_access', $vocabulary_alias);
  499. // Also, the taxonomy term entity exposes a bundle, but doesn't have a bundle
  500. // column in the database. We have to alter the query ourself to go fetch
  501. // the bundle.
  502. $conditions = &$query->conditions();
  503. foreach ($conditions as $key => &$condition) {
  504. if ($key !== '#conjunction' && is_string($condition['field']) && $condition['field'] === 'vocabulary_machine_name') {
  505. $condition['field'] = $vocabulary_alias . '.machine_name';
  506. break;
  507. }
  508. }
  509. }
  510. /**
  511. * Implements EntityReferenceHandler::getReferencableEntities().
  512. */
  513. public function getReferencableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
  514. if ($match || $limit) {
  515. return parent::getReferencableEntities($match , $match_operator, $limit);
  516. }
  517. $options = array();
  518. $entity_type = $this->field['settings']['target_type'];
  519. // We imitate core by calling taxonomy_get_tree().
  520. $entity_info = entity_get_info('taxonomy_term');
  521. $bundles = !empty($this->field['settings']['handler_settings']['target_bundles']) ? $this->field['settings']['handler_settings']['target_bundles'] : array_keys($entity_info['bundles']);
  522. foreach ($bundles as $bundle) {
  523. if ($vocabulary = taxonomy_vocabulary_machine_name_load($bundle)) {
  524. if ($terms = taxonomy_get_tree($vocabulary->vid, 0)) {
  525. foreach ($terms as $term) {
  526. $options[$vocabulary->machine_name][$term->tid] = str_repeat('-', $term->depth) . check_plain($term->name);
  527. }
  528. }
  529. }
  530. }
  531. return $options;
  532. }
  533. }