CerFieldChain.inc 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * @file
  4. * Contains the CerFieldChain class.
  5. */
  6. class CerFieldChain extends FieldChain {
  7. /**
  8. * Convenience method. Returns a handler for this chain in the context of
  9. * the given entity.
  10. *
  11. * @return CerFieldChainHandler
  12. */
  13. public function getHandler(EntityDrupalWrapper $entity) {
  14. return new CerFieldChainHandler($this, $entity);
  15. }
  16. /**
  17. * Gets a regular expression to match field chain identifiers that this chain
  18. * can reference, e.g. /^node:(page|article):/
  19. */
  20. public function regex() {
  21. $end = $this->end();
  22. return '/^' . $end->getTargetType() . ':(' . implode('|', $end->getTargetBundles()) . '):/';
  23. }
  24. /**
  25. * Returns a Features export pipe for this chain, including every field and
  26. * field instance in it.
  27. */
  28. public function export() {
  29. $pipe = array();
  30. foreach ($this->chain as $field) {
  31. $pipe['field_instance'][] = "{$field->entityType}-{$field->bundle}-{$field->name}";
  32. }
  33. return $pipe;
  34. }
  35. /**
  36. * Returns an array of every possible field chain for every field defined in
  37. * hook_cer_fields().
  38. *
  39. * @return array
  40. */
  41. public static function collectAll() {
  42. $chains = array();
  43. foreach (array_keys(CerField::getPluginInfo()) as $identifier) {
  44. $chains = array_merge($chains, self::collect($identifier));
  45. }
  46. return $chains;
  47. }
  48. /**
  49. * Returns an array of every possible field chain for a single field,
  50. * identified by its key in hook_cer_fields().
  51. *
  52. * @return array
  53. */
  54. public static function collect($identifier) {
  55. $chains = array();
  56. $chain = new CerFieldChain();
  57. $chain->addField(CerField::getPlugin($identifier), $chains);
  58. return $chains;
  59. }
  60. /**
  61. * Constructs and returns a CerFieldChain object from an encoded string
  62. * of field plugin identifiers glued together with ::.
  63. *
  64. * @return CerFieldChain
  65. */
  66. public static function unpack($identifier) {
  67. $chain = new CerFieldChain();
  68. foreach (array_reverse(explode('::', $identifier)) as $field) {
  69. $chain->addField(CerField::getPlugin($field));
  70. }
  71. return $chain;
  72. }
  73. }