This repository has been archived by the owner on Mar 29, 2018. It is now read-only.
/
Field.php
111 lines (102 loc) · 2.9 KB
/
Field.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
namespace Drupal\little_helpers;
/**
* OOP-wrapper for the data-structure used by field_*_field() functions.
*/
class Field {
public $id = NULL;
public $entity_types = array();
public $field_name;
public $type;
public $module;
public $active = 1;
public $storage = array();
public $locked = FALSE;
public $cardinality = 1;
public $translatable = FALSE;
public $deleted = 0;
public $settings = array();
public static function byName($name) {
$class = \get_called_class();
if ($data = \field_read_field($name)) {
return new $class($data);
}
return FALSE;
}
public static function fromType($type, $name = NULL) {
$class = \get_called_class();
$new = new $class(array('field_name' => $name));
$new->setType($type);
return $new;
}
public function __construct($data) {
foreach ($data as $k => $v) {
$this->$k = $v;
}
}
/**
* Load default data for this field-type.
*
* @see \field_create_field().
*/
public function setType($type) {
$field_type = \field_info_field_types($type);
$this->settings += \field_info_field_settings($type);
$this->module = $field_type['module'];
$this->type = $type;
}
/**
* Save field configuration to database.
*
* @see \field_update_field().
* @see \field_create_field().
*/
public function save() {
if (isset($this->id)) {
\field_update_field((array) $this);
} else {
foreach (\field_create_field((array) $this) as $k => $v) {
$this->$k = $v;
}
}
return $this;
}
/**
* Delete an existing field.
*
* @see \field_delete_field().
*/
public function delete() {
// @FIXME
// Fields and field instances are now exportable configuration entities, and
// the Field Info API has been removed.
//
//
// @see https://www.drupal.org/node/2012896
// field_delete_field($this->field_name);
}
/**
* Change the machine name of an existing field.
*
* @param $newName string
*
* NOTE: This might need additional adjustments for contrib modules
* that store field_names (ie. views, context, cck_blocks).
*/
public function rename($newName) {
$o = $this->field_name;
$n = $newName;
db_query("UPDATE field_config SET field_name='$n' WHERE field_name='$o'");
db_query("UPDATE field_config_instance SET field_name='$n' WHERE field_name='$o'");
db_query("RENAME TABLE `field_data_$o` TO `field_data_$n`;");
db_query("RENAME TABLE `field_revision_$o` TO `field_revision_$n`;");
\module_load_install($this->module);
$function = $this->module . '_field_schema';
$schema = $function(array('type' => $this->type));
foreach ($schema['columns'] as $column => $specs) {
db_change_field("field_data_$n", "{$o}_{$column}", "{$n}_{$column}", $specs);
db_change_field("field_revision_$n", "{$o}_{$column}", "{$n}_{$column}", $specs);
}
$this->field_name = $n;
}
}