Projet

Général

Profil

Paste
Télécharger (2,91 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / link / link.migrate.inc @ a5572547

1 85ad3d82 Assos Assos
<?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
 * @endcode
15
 *
16
 * With earlier versions of Migrate, you must pass an arguments array:
17
 *
18
 * @code
19
 * $link_args = array(
20
 *   'title' => array('source_field' => 'source_title'),
21
 *   'attributes' => array('source_field' => 'source_attributes'),
22
 * );
23
 * $this->addFieldMapping('field_my_link', 'source_url')
24
 *      ->arguments($link_args);
25
 * @endcode
26
 */
27
28
/**
29
 * Implements hook_migrate_api().
30
 */
31
function link_migrate_api() {
32
  return array(
33
    'api' => 2,
34
    'field handlers' => array('MigrateLinkFieldHandler'),
35
  );
36
}
37
38
class MigrateLinkFieldHandler extends MigrateFieldHandler {
39
  public function __construct() {
40
    $this->registerTypes(array('link_field'));
41
  }
42
43
  static function arguments($title = NULL, $attributes = NULL, $language = NULL) {
44
    $arguments = array();
45
    if (!is_null($title)) {
46
      $arguments['title'] = $title;
47
    }
48
    if (!is_null($attributes)) {
49
      $arguments['attributes'] = $attributes;
50
    }
51
    if (!is_null($language)) {
52
      $arguments['language'] = $language;
53
    }
54
    return $arguments;
55
  }
56
57
  /**
58
   * Implementation of MigrateFieldHandler::fields().
59
   *
60
   * @param $type
61
   *  The field type.
62
   * @param $instance
63
   *  Instance info for the field.
64
   * @param Migration $migration
65
   *  The migration context for the parent field. We can look at the mappings
66
   *  and determine which subfields are relevant.
67
   * @return array
68
   */
69
  public function fields($type, $instance, $migration = NULL) {
70
    return array(
71
      'title' => t('Subfield: The link title attribute'),
72
      'attributes' => t('Subfield: The attributes for this link'),
73
      'language' => t('Subfield: The language for the field'),
74
    );
75
  }
76
77
  public function prepare($entity, array $field_info, array $instance, array $values) {
78
    if (isset($values['arguments'])) {
79
      $arguments = $values['arguments'];
80
      unset($values['arguments']);
81
    }
82
    else {
83
      $arguments = array();
84
    }
85
86
    $language = $this->getFieldLanguage($entity, $field_info, $arguments);
87
    $values = array_filter($values);
88
89
    foreach ($values as $delta => $value) {
90
      $item = array();
91
      if (isset($arguments['title'])) {
92
        if (!is_array($arguments['title'])) {
93
          $item['title'] = $arguments['title'];
94
        }
95
        elseif (isset($arguments['title'][$delta])) {
96
          $item['title'] = $arguments['title'][$delta];
97
        }
98
      }
99
      if (isset($arguments['attributes'])) {
100
        $item['attributes'] = $arguments['attributes'];
101
      }
102
      $item['url'] = $value;
103
      $return[$language][$delta] = $item;
104
    }
105
106
    return isset($return) ? $return : NULL;
107
  }
108
}