AbstractFieldSynonymsBehavior.class.inc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * @file
  4. * Definition of AbstractFieldSynonymsBehavior class.
  5. */
  6. /**
  7. * Abstract class for providing synonyms from fields attached to entities.
  8. */
  9. abstract class AbstractFieldSynonymsBehavior extends AbstractSynonymsBehavior {
  10. /**
  11. * Field definition array on which this provider was initialized.
  12. *
  13. * @var array
  14. */
  15. protected $field;
  16. /**
  17. * Field instance definition on which this provider was initialized.
  18. *
  19. * @var array
  20. */
  21. protected $instance;
  22. public function __construct($behavior_implementation) {
  23. parent::__construct($behavior_implementation);
  24. $this->field = field_info_field(synonyms_provider_field_field_name($behavior_implementation['provider']));
  25. $this->instance = field_info_instance($behavior_implementation['entity_type'], $this->field['field_name'], $behavior_implementation['bundle']);
  26. }
  27. public function featuresExportPipe() {
  28. $pipe = parent::featuresExportPipe();
  29. // We include the Features component for the underlying field on which this
  30. // synonyms provider is built.
  31. $pipe['field'][] = implode('-', array($this->behavior_implementation['entity_type'], $this->behavior_implementation['bundle'], $this->field['field_name']));
  32. return $pipe;
  33. }
  34. /**
  35. * Retrieve items of the underlying field in this behavior implementation.
  36. *
  37. * @param object $entity
  38. * Entity whose items should be retrieved
  39. * @param string $langcode
  40. * Language code for which to retrieve items from the entity, if one is
  41. * known
  42. *
  43. * @return array
  44. * Array of items that provided entity has in the field on which behavior
  45. * implementation is set up
  46. */
  47. protected function entityItems($entity, $langcode = NULL) {
  48. $items = field_get_items($this->instance['entity_type'], $entity, $this->field['field_name'], $langcode);
  49. return is_array($items) ? $items : array();
  50. }
  51. /**
  52. * Filter $items only to contain unique values.
  53. *
  54. * @param array $items
  55. * Array of field items that should be filtered to contain only unique
  56. * values
  57. * @param array $unique_index
  58. * Array of column names that define uniqueness for an item
  59. *
  60. * @return array
  61. * Only unique items from the provided $items array
  62. */
  63. protected function uniqueItems($items, $unique_index) {
  64. $index = array();
  65. foreach ($items as $item) {
  66. $item_index = array();
  67. foreach ($unique_index as $column) {
  68. $item_index[] = is_scalar($column) ? $column : serialize($column);
  69. }
  70. $index[serialize($item_index)] = $item;
  71. }
  72. return array_values($index);
  73. }
  74. }