Projet

Général

Profil

Paste
Télécharger (3,53 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / link / link.migrate.inc @ 39a181a4

1
<?php
2

    
3
/**
4
 * @file
5
 * Support for migrate module.
6
 *
7
 * With Migrate 2.4 or later, you can use the subfield syntax to set the title
8
 * and attributes:
9
 *
10
 * @code
11
 * $this->addFieldMapping('field_my_link', 'source_url');
12
 * $this->addFieldMapping('field_my_link:title', 'source_title');
13
 * $this->addFieldMapping('field_my_link:attributes', 'source_attributes');
14
 *
15
 * # With earlier versions of Migrate, you must pass an arguments array:
16
 *
17
 * $link_args = array(
18
 *   'title' => array('source_field' => 'source_title'),
19
 *   'attributes' => array('source_field' => 'source_attributes'),
20
 * );
21
 * $this->addFieldMapping('field_my_link', 'source_url')
22
 *      ->arguments($link_args);
23
 * @endcode
24
 */
25

    
26
if (!class_exists('MigrateFieldHandler')) {
27
  return;
28
}
29

    
30
/**
31
 * Implements hook_migrate_api().
32
 */
33
function link_migrate_api() {
34
  return array(
35
    'api' => 2,
36
    'field handlers' => array('MigrateLinkFieldHandler'),
37
  );
38
}
39

    
40
// @codingStandardsIgnoreLine
41
class MigrateLinkFieldHandler extends MigrateFieldHandler {
42

    
43
  /**
44
   * Construct.
45
   */
46
  public function __construct() {
47
    $this->registerTypes(array('link_field'));
48
  }
49

    
50
  /**
51
   * Arguments.
52
   */
53
  public static function arguments($title = NULL, $attributes = NULL, $language = NULL) {
54
    $arguments = array();
55
    if (!is_null($title)) {
56
      $arguments['title'] = $title;
57
    }
58
    if (!is_null($attributes)) {
59
      $arguments['attributes'] = $attributes;
60
    }
61
    if (!is_null($language)) {
62
      $arguments['language'] = $language;
63
    }
64
    return $arguments;
65
  }
66

    
67
  /**
68
   * Implementation of MigrateFieldHandler::fields().
69
   *
70
   * @param array $type
71
   *   The field type.
72
   * @param array $instance
73
   *   Instance info for the field.
74
   * @param Migration $migration
75
   *   The migration context for the parent field. We can look at the mappings
76
   *   and determine which subfields are relevant.
77
   *
78
   * @return array
79
   *   Array with values.
80
   *
81
   * @codingStandardsIgnoreStart
82
   */
83
  public function fields($type, $instance, $migration = NULL) {
84
    // @codingStandardsIgnoreEnd
85
    return array(
86
      'title' => t('Subfield: The link title attribute'),
87
      'attributes' => t('Subfield: The attributes for this link'),
88
      'language' => t('Subfield: The language for the field'),
89
    );
90
  }
91

    
92
  /**
93
   * Prepare.
94
   */
95
  public function prepare($entity, array $field_info, array $instance, array $values) {
96
    if (isset($values['arguments'])) {
97
      $arguments = $values['arguments'];
98
      unset($values['arguments']);
99
    }
100
    else {
101
      $arguments = array();
102
    }
103

    
104
    $language = $this->getFieldLanguage($entity, $field_info, $arguments);
105
    $values = array_filter($values);
106

    
107
    foreach ($values as $delta => $value) {
108
      $item = array();
109
      if (isset($arguments['title'])) {
110
        if (!is_array($arguments['title'])) {
111
          $item['title'] = $arguments['title'];
112
        }
113
        elseif (isset($arguments['title'][$delta])) {
114
          $item['title'] = $arguments['title'][$delta];
115
        }
116
      }
117
      if (isset($arguments['attributes'])) {
118
        if (is_array($arguments['attributes']) && isset($arguments['attributes'][$delta])) {
119
          $item['attributes'] = $arguments['attributes'][$delta];
120
        }
121
        else {
122
          $item['attributes'] = $arguments['attributes'];
123
        }
124
      }
125
      $item['url'] = $value;
126

    
127
      if (is_array($language)) {
128
        $current_language = $language[$delta];
129
      }
130
      else {
131
        $current_language = $language;
132
      }
133
      $return[$current_language][$delta] = $item;
134
    }
135

    
136
    return isset($return) ? $return : NULL;
137
  }
138

    
139
}