Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d7 / hook / options_list.twig
diff --git a/vendor/chi-teck/drupal-code-generator/templates/d7/hook/options_list.twig b/vendor/chi-teck/drupal-code-generator/templates/d7/hook/options_list.twig
new file mode 100644 (file)
index 0000000..0934dc8
--- /dev/null
@@ -0,0 +1,40 @@
+/**
+ * Implements hook_options_list().
+ */
+function {{ machine_name }}_options_list($field, $instance, $entity_type, $entity) {
+  // Sample structure.
+  $options = array(
+    0 => t('Zero'),
+    1 => t('One'),
+    2 => t('Two'),
+    3 => t('Three'),
+  );
+
+  // Sample structure with groups. Only one level of nesting is allowed. This
+  // is only supported by the 'options_select' widget. Other widgets will
+  // flatten the array.
+  $options = array(
+    t('First group') => array(
+      0 => t('Zero'),
+    ),
+    t('Second group') => array(
+      1 => t('One'),
+      2 => t('Two'),
+    ),
+    3 => t('Three'),
+  );
+
+  // In actual implementations, the array of options will most probably depend
+  // on properties of the field. Example from taxonomy.module:
+  $options = array();
+  foreach ($field['settings']['allowed_values'] as $tree) {
+    $terms = taxonomy_get_tree($tree['vid'], $tree['parent']);
+    if ($terms) {
+      foreach ($terms as $term) {
+        $options[$term->tid] = str_repeat('-', $term->depth) . $term->name;
+      }
+    }
+  }
+
+  return $options;
+}