summaryrefslogtreecommitdiff
path: root/sites/all/modules/media_gallery/fields_rsi_prevention.inc
diff options
context:
space:
mode:
Diffstat (limited to 'sites/all/modules/media_gallery/fields_rsi_prevention.inc')
-rw-r--r--sites/all/modules/media_gallery/fields_rsi_prevention.inc55
1 files changed, 55 insertions, 0 deletions
diff --git a/sites/all/modules/media_gallery/fields_rsi_prevention.inc b/sites/all/modules/media_gallery/fields_rsi_prevention.inc
new file mode 100644
index 000000000..374b35899
--- /dev/null
+++ b/sites/all/modules/media_gallery/fields_rsi_prevention.inc
@@ -0,0 +1,55 @@
+<?php
+
+/**
+ * @file
+ * This file provides easier access on entity properties and methods.
+ */
+
+/**
+ * Decorates an entity to provide getters/setters.
+ *
+ * @example
+ *
+ * $node = new FieldRSIPreventor($node);
+ *
+ * // This still works,
+ * $node->created
+ *
+ * // Gets the first value of body for LANGUAGE_NONE.
+ * $node->getValue('body');
+ *
+ * // Gets the 2nd value of body in spanish
+ * $node->getValue('body', 2, 'esp');
+ */
+class FieldsRSIPreventor {
+ private $entity;
+
+ function __construct($entity) {
+ // Prevent this thing from chaining if people accidentally use it twice.
+ if ($entity instanceof FieldRSIPreventor) {
+ $entity = $entity->entity;
+ }
+ $this->entity = $entity;
+ }
+
+ function getValue($field_name, $delta = 0, $language = LANGUAGE_NONE) {
+ if ($item = $this->getItem($field_name, $delta, $language)) {
+ return $item['value'];
+ }
+ }
+
+ function getItem($field_name, $delta = 0, $language = LANGUAGE_NONE) {
+ if (!isset($this->entity->{$field_name}[$language]) || !isset($this->entity->{$field_name}[$language][$delta])) {
+ return FALSE;
+ }
+ return $this->entity->{$field_name}[$language][$delta];
+ }
+
+ function __get($key) {
+ return $this->entity->{$key};
+ }
+
+ function __set($key, $value) {
+ $this->entity->{$key} = $value;
+ }
+}