updated some more modules
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
name = "Field Object"
|
||||
description = "Provides a field type that can refer to field instances."
|
||||
|
||||
core = "7.x"
|
||||
|
||||
dependencies[] = options
|
||||
|
||||
files[] = includes/FieldChain.inc
|
||||
files[] = includes/FieldHierarchy.inc
|
||||
files[] = includes/FieldInstance.inc
|
||||
|
||||
; Information added by Drupal.org packaging script on 2016-02-12
|
||||
version = "7.x-3.0-alpha7+25-dev"
|
||||
core = "7.x"
|
||||
project = "cer"
|
||||
datestamp = "1455299962"
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Implements hook_field_schema().
|
||||
*/
|
||||
function field_object_field_schema(array $field) {
|
||||
return array(
|
||||
'columns' => array(
|
||||
'path' => array(
|
||||
'type' => 'text',
|
||||
'size' => 'tiny',
|
||||
'description' => 'The path to the instance, represented as text.',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fixes issue #2331553, and probably others.
|
||||
*/
|
||||
function field_object_update_7001() {
|
||||
db_update('field_config')
|
||||
->fields(array(
|
||||
'type' => 'field_object',
|
||||
'module' => 'field_object',
|
||||
))
|
||||
->condition('field_name', array('cer_left', 'cer_right'))
|
||||
->execute();
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Implements hook_theme().
|
||||
*/
|
||||
function field_object_theme() {
|
||||
return array(
|
||||
'field_object_label' => array(
|
||||
'variables' => array(
|
||||
'field' => array(),
|
||||
'instance' => array(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_field_info().
|
||||
*/
|
||||
function field_object_field_info() {
|
||||
return array(
|
||||
'field_object' => array(
|
||||
'label' => t('Field Reference'),
|
||||
'description' => t('Refers to a field instance.'),
|
||||
'settings' => array(),
|
||||
'instance_settings' => array(
|
||||
'function' => NULL,
|
||||
),
|
||||
'default_widget' => 'options_select',
|
||||
'default_formatter' => 'field_object_label',
|
||||
'no_ui' => TRUE,
|
||||
'property_type' => 'text',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_field_widget_info_alter().
|
||||
*/
|
||||
function field_object_field_widget_info_alter(array &$info) {
|
||||
$info['options_select']['field types'][] = 'field_config_reference';
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_field_formatter_info().
|
||||
*/
|
||||
function field_object_field_formatter_info() {
|
||||
return array(
|
||||
'field_object_label' => array(
|
||||
'label' => t('Label'),
|
||||
'description' => t("The field instance's label."),
|
||||
'field types' => array('field_object'),
|
||||
'settings' => array(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_field_is_empty().
|
||||
*/
|
||||
function field_object_field_is_empty(array $item, array $field) {
|
||||
return empty($item['path']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_field_formatter_view().
|
||||
*/
|
||||
function field_object_field_formatter_view($entity_type, $entity, array $field, array $instance, $language, array $items, array $display) {
|
||||
$element = array();
|
||||
|
||||
foreach ($items as $delta => $item) {
|
||||
$label = array();
|
||||
|
||||
foreach (_field_object_expand_path($item['path']) as $field) {
|
||||
$label[] = theme('field_object_label', $field);
|
||||
}
|
||||
|
||||
$element[$delta]['#markup'] = implode(' » ', $label);
|
||||
}
|
||||
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_options_list().
|
||||
*/
|
||||
function field_object_options_list(array $field, array $instance, $entity_type, $entity) {
|
||||
return _field_object_build_hierarchy($field, $instance, $entity_type, $entity)->options();
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders a human-readable label for a field instance, including the entity
|
||||
* type and (if applicable) bundle that hosts it.
|
||||
*/
|
||||
function theme_field_object_label(array $variables) {
|
||||
$instance = $variables['instance'];
|
||||
|
||||
$output = '';
|
||||
|
||||
if ($instance['entity_type'] != 'field_collection_item') {
|
||||
$entity_type = entity_get_info($instance['entity_type']);
|
||||
$output = $entity_type['label'] . ' » ';
|
||||
|
||||
if ($entity_type['entity keys']['bundle']) {
|
||||
$output .= $entity_type['bundles'][ $instance['bundle'] ]['label'] . ' » ';
|
||||
}
|
||||
}
|
||||
|
||||
return $output . $instance['label'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function. Builds a FieldHierarchy object for the widget builder.
|
||||
*/
|
||||
function _field_object_build_hierarchy(array $field, array $instance, $entity_type, $entity) {
|
||||
$hierarchy = new FieldHierarchy();
|
||||
|
||||
// The instance should define a function which returns an array of FieldChain
|
||||
// objects to be added to the hierarchy.
|
||||
$function = $instance['settings']['function'];
|
||||
|
||||
if ($function && is_callable($function)) {
|
||||
$arguments = func_get_args();
|
||||
$chains = (array) call_user_func_array($function, $arguments);
|
||||
|
||||
array_walk($chains, array($hierarchy, 'addChain'));
|
||||
}
|
||||
|
||||
return $hierarchy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function. Expands a field reference's path value into an array
|
||||
* of field and instance definitions.
|
||||
*/
|
||||
function _field_object_expand_path($path) {
|
||||
$output = array();
|
||||
|
||||
foreach (explode('::', $path) as $instance) {
|
||||
list ($entity_type, $bundle, $field) = explode(':', $instance);
|
||||
|
||||
$output[] = array(
|
||||
'field' =>
|
||||
field_info_field($field),
|
||||
'instance' =>
|
||||
field_info_instance($entity_type, $field, $bundle),
|
||||
);
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains the FieldChain class.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @class
|
||||
* A doubly linked list of FieldInstance objects.
|
||||
*/
|
||||
class FieldChain implements SeekableIterator {
|
||||
|
||||
protected $chain = array();
|
||||
|
||||
protected $index = 0;
|
||||
|
||||
/**
|
||||
* Magic post-unserialization callback. Provides every field in the chain
|
||||
* with a reference to its parent (if any) and child (if any), effectively
|
||||
* turning the chain into a doubly linked list.
|
||||
*/
|
||||
public function __wakeup() {
|
||||
foreach ($this->chain as $field) {
|
||||
if (isset($parent)) {
|
||||
$field->parent($parent)->child($field);
|
||||
}
|
||||
$parent = $field;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents this chain as a machine-readable string, separating the fields
|
||||
* with a T_PAAMAYIM_NEKUDOTAYIM (or, as we call it on planet Earth, a
|
||||
* double colon).
|
||||
*/
|
||||
public function __toString() {
|
||||
$key = array();
|
||||
|
||||
foreach ($this->chain as $field) {
|
||||
$key[] = $field->__toString();
|
||||
}
|
||||
|
||||
return implode('::', $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepends a field instance to this chain. If $completed is passed, we'll
|
||||
* try to find the parents of the instance and recurse upwards, building
|
||||
* a tree of "routes" to the instance.
|
||||
*/
|
||||
public function addField(FieldInstance $field, array &$completed = NULL) {
|
||||
array_unshift($this->chain, $field);
|
||||
$this->__wakeup();
|
||||
|
||||
if (isset($completed)) {
|
||||
$parents = $field->getParents();
|
||||
|
||||
if ($parents) {
|
||||
foreach ($parents as $parent) {
|
||||
$branch = clone $this;
|
||||
$branch->addField($parent, $completed);
|
||||
}
|
||||
}
|
||||
else {
|
||||
$completed[] = $this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last field in the chain.
|
||||
*/
|
||||
public function end() {
|
||||
return end($this->chain);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements SeekableIterator::seek().
|
||||
*/
|
||||
public function seek($position) {
|
||||
if ($position >= 0 && $position < sizeof($this->chain)) {
|
||||
$this->index = $position;
|
||||
}
|
||||
else {
|
||||
throw new OutOfBoundsException(t('Cannot seek to invalid position %position.', array('%position' => $position)));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements Iterator::current().
|
||||
*/
|
||||
public function current() {
|
||||
return $this->chain[$this->index];
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements Iterator::key().
|
||||
*/
|
||||
public function key() {
|
||||
return $this->current()->__toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements Iterator::next().
|
||||
*/
|
||||
public function next() {
|
||||
$this->index++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements Iterator::rewind().
|
||||
*/
|
||||
public function rewind() {
|
||||
$this->index = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements Iterator::valid().
|
||||
*/
|
||||
public function valid() {
|
||||
return ($this->index < sizeof($this->chain));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains the FieldHierarchy class.
|
||||
*/
|
||||
|
||||
class FieldHierarchy implements Countable {
|
||||
|
||||
/**
|
||||
* @var array
|
||||
* The flattened hierarchy data.
|
||||
*/
|
||||
protected $data = array();
|
||||
|
||||
const ROOT = 'root';
|
||||
|
||||
public function __get($property) {
|
||||
return $this->{$property};
|
||||
}
|
||||
|
||||
public function options($key = FieldHierarchy::ROOT, $parent = NULL, $depth = -1) {
|
||||
$options = array();
|
||||
|
||||
$item = $this->data[$key];
|
||||
|
||||
if (isset($item['label'])) {
|
||||
$options[$key] = str_repeat('-', $depth) . $item['label'];
|
||||
}
|
||||
|
||||
if (isset($item['children'])) {
|
||||
foreach ($item['children'] as $child) {
|
||||
$options = array_merge($options, $this->options($child, $key, $depth + 1));
|
||||
}
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an item of any type to the hierarchy.
|
||||
*/
|
||||
public function add($item_key, $label = NULL, $parent = FieldHierarchy::ROOT) {
|
||||
if (!array_key_exists($item_key, $this->data)) {
|
||||
$this->data[$item_key]['label'] = isset($label) ? $label : $item_key;
|
||||
}
|
||||
|
||||
if (!isset($this->data[$parent]['children'])) {
|
||||
$this->data[$parent]['children'] = array();
|
||||
}
|
||||
|
||||
if (!in_array($item_key, $this->data[$parent]['children'])) {
|
||||
$this->data[$parent]['children'][] = $item_key;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a single field plugin to the hierarchy.
|
||||
*/
|
||||
public function addField(FieldInstance $field) {
|
||||
$bundle_key = "{$field->entityType}:{$field->bundle}";
|
||||
|
||||
if ($field->isBundleable) {
|
||||
$this->add($field->entityType, $field->entityTypeLabel);
|
||||
$this->add($bundle_key, $field->bundleLabel, $field->entityType);
|
||||
}
|
||||
else {
|
||||
$this->add($bundle_key, $field->entityTypeLabel);
|
||||
}
|
||||
|
||||
$field_key = "{$bundle_key}:{$field->name}";
|
||||
$this->add($field_key, $field->label, $bundle_key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an entire field chain to the hierarchy.
|
||||
*/
|
||||
public function addChain(FieldChain $chain) {
|
||||
$parents = array();
|
||||
|
||||
foreach ($chain as $field) {
|
||||
if ($field->requireParent()) {
|
||||
$parent_key = implode('::', $parents);
|
||||
$field_key = "{$parent_key}::{$field}";
|
||||
|
||||
$this->add($field_key, $field->label, $parent_key);
|
||||
}
|
||||
else {
|
||||
$this->addField($field);
|
||||
}
|
||||
|
||||
$parents[] = $field->__toString();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @implements Countable::count().
|
||||
*/
|
||||
public function count() {
|
||||
return sizeof($this->data);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
class FieldInstance {
|
||||
|
||||
/**
|
||||
* @var string
|
||||
* The instance's entity type.
|
||||
*/
|
||||
public $entityType;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
* The instance bundle.
|
||||
*/
|
||||
public $bundle;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
* The field's machine name.
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var boolean
|
||||
* Whether or not this instance's entity type supports bundles.
|
||||
*/
|
||||
public $isBundleable;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
* The human-readable label of the instance's entity type.
|
||||
*/
|
||||
public $entityTypeLabel;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
* The human-readable label of the instance's bundle.
|
||||
*/
|
||||
public $bundleLabel;
|
||||
|
||||
/**
|
||||
* @var integer
|
||||
* The cardinality (maximum values) the field supports, or
|
||||
* FIELD_CARDINALITY_UNLIMITED.
|
||||
*/
|
||||
public $cardinality;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
* The instance's label.
|
||||
*/
|
||||
public $label;
|
||||
|
||||
/**
|
||||
* @var FieldInstance
|
||||
* The parent of this instance, if any.
|
||||
*/
|
||||
protected $parent;
|
||||
|
||||
/**
|
||||
* @var FieldInstance
|
||||
* The child of this instance, if any.
|
||||
*/
|
||||
protected $child;
|
||||
|
||||
public function __construct($entity_type, $bundle, $field_name) {
|
||||
$this->entityType = $entity_type;
|
||||
$this->bundle = $bundle;
|
||||
$this->name = $field_name;
|
||||
|
||||
// Get info about the entity type and bundle hosting this field instance.
|
||||
$info = entity_get_info($entity_type);
|
||||
$this->isBundleable = (boolean) $info['entity keys']['bundle'];
|
||||
$this->entityTypeLabel = $info['label'];
|
||||
$this->bundleLabel = $info['bundles'][$bundle]['label'];
|
||||
|
||||
// Get global info about the field.
|
||||
$info = field_info_field($field_name);
|
||||
$this->cardinality = $info['cardinality'];
|
||||
|
||||
// Finally, get info about the field instance.
|
||||
$instance = field_info_instance($entity_type, $field_name, $bundle);
|
||||
$this->label = $instance['label'];
|
||||
}
|
||||
|
||||
public function __toString() {
|
||||
return "{$this->entityType}:{$this->bundle}:{$this->name}";
|
||||
}
|
||||
|
||||
/**
|
||||
* Get or set the parent of this field instance.
|
||||
*/
|
||||
public function parent(FieldInstance $parent = NULL) {
|
||||
if ($parent) {
|
||||
$this->parent = $parent;
|
||||
}
|
||||
return $this->parent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get or set the child of this field instance.
|
||||
*/
|
||||
public function child(FieldInstance $child = NULL) {
|
||||
if ($child) {
|
||||
$this->child = $child;
|
||||
}
|
||||
return $this->child;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if this field requires a parent. An example of this would be
|
||||
* a field that is instantiated on a field collection (which is itself
|
||||
* a field).
|
||||
*/
|
||||
public function requireParent() {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the parents of this field instance as an array of FieldInstance
|
||||
* objects. If there are no parents, return an empty array.
|
||||
*/
|
||||
public function getParents() {
|
||||
return array();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user