View source
<?php
namespace Drupal\little_helpers\Field;
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 byType($type) {
$fields = [];
foreach (\field_read_fields([
'type' => $type,
]) as $info) {
$fields[$info['field_name']] = new static($info);
}
return $fields;
}
public static function byName($name) {
if ($data = \field_read_field($name)) {
return new static($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;
}
}
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;
}
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;
}
public function delete() {
field_delete_field($this->field_name);
}
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) $this);
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;
}
}