/** * Find a label for the given field */ protected function findFieldLabel($fieldName) { $map = field_info_field_map(); if (!empty($map[$fieldName]['bundles'])) { foreach ($map[$fieldName]['bundles'] as $type => $bundles) { foreach ($bundles as $bundle) { $instance = field_info_instance($type, $fieldName, $bundle); if (!empty($instance['label']) && $instance['label'] !== $fieldName) { return $instance['label']; } } } } }
/** * Generate a settings form for this handler. */ public function settingsForm($field, $instance) { // Current settings. $settings = $instance['settings']['behaviors']['autofill']; $widget = $instance['widget']; // Load available fields from instance bundle. $entity_type = $instance['entity_type']; $bundle_name = $instance['bundle']; $bundle_info = field_info_instances($entity_type, $bundle_name); // Field target metadata. $field_map = field_info_field_map(); $target_type = $field['settings']['target_type']; $target_bundles = isset($field['settings']['handler_settings']['target_bundles']); $target_bundles = $target_bundles ? $field['settings']['handler_settings']['target_bundles'] : array(); // Skip autofill for the reference field. unset($bundle_info[$field['field_name']]); // Create checkboxes options for available fields. $field_options = array(); foreach ($bundle_info as $field_name => $field_info) { if (!empty($field_map[$field_name]['bundles'][$target_type])) { $available_bundles = $field_map[$field_name]['bundles'][$target_type]; // Determine all targeted bundles that use this field. $option_bundles = empty($target_bundles) ? $available_bundles : array_intersect($target_bundles, $available_bundles); if (!empty($option_bundles)) { $field_options[$field_name] = t('@field_label (@field_name): <em>Available in bundle(s) @bundles</em>', array('@field_label' => $field_info['label'], '@field_name' => $field_name, '@bundles' => implode(', ', $option_bundles))); } } } $form['overwrite'] = array('#type' => 'checkbox', '#title' => t('Overwrite existing data'), '#description' => t('Select if you want to overwrite fields that already have values. <br/><em><strong>NOTE:</strong> Disabling this is experimental and might not work 100%. If you experience issues with fields being overridden nonetheless, please report what field type and settings this occurs on in the modules issue queue on drupal.org</em>'), '#default_value' => isset($settings['overwrite']) ? $settings['overwrite'] : 1); if (empty($field_options)) { $no_fields_found = t('There are no common fields between this bundle and its referenced entities.'); $usage_instructions = t('To use autofill, you need to add instances of the same fields to its referenced bundles.'); $form['no_fields'] = array('#markup' => '<div class="messages warning">' . $no_fields_found . $usage_instructions . '</div>', '#type' => 'item'); } else { $form['fields'] = array('#type' => 'checkboxes', '#title' => t('Fields'), '#options' => $field_options, '#description' => t('Select which fields from the referenced entity you want to load on changing the value of this field.'), '#default_value' => isset($settings['fields']) ? $settings['fields'] : array()); } return $form; }
/** * Grab a list of all fields. * * This is simply a wrapper around field_info_field_map(). * * @return array * Information about an entity type, or all entities if one was passed in. */ public function grabFieldList() { return field_info_field_map(); }
// from http://drupal.stackexchange.com/questions/79378/changing-a-field-type-from-integer-to-decimal // Change this to your field name, obvs. $field = 'field_height'; // Update the storage tables $tables = array('field_data', 'field_revision'); foreach ($tables as $table) { $tablename = $table . '_' . $field; $fieldname = $field . '_value'; db_change_field($tablename, $fieldname, $fieldname, array('type' => 'numeric', 'precision' => 10, 'scale' => 1, 'not null' => FALSE)); } // Fetch the current field configuration $field_config = db_query("SELECT data FROM {field_config} WHERE field_name = :field_name", array(':field_name' => $field))->fetchObject(); $data = unserialize($field_config->data); // Update the settings entry $data['settings'] = array('precision' => 10, 'scale' => 1, 'decimal_separator' => '.'); // Store the new field config, update the field type at the same time db_update('field_config')->fields(array('data' => serialize($data), 'type' => 'number_decimal'))->condition('field_name', $field)->execute(); // If you are confident about what bundles have instances of this field you can // go straight to the db_query with a hardcoded entity_type / bundle. $instances = field_info_field_map(); foreach ($instances[$field]['bundles'] as $entity_type => $bundles) { foreach ($bundles as $bundle) { // Fetch the field instance data $field_config_instance = db_query("SELECT data FROM {field_config_instance}\n WHERE field_name = :field_name\n AND entity_type = :entity_type\n AND bundle = :bundle", array(':field_name' => $field, ':entity_type' => $entity_type, ':bundle' => $bundle))->fetchObject(); $data = unserialize($field_config_instance->data); // Update it with the new display type $data['display']['default']['type'] = 'number_decimal'; // Store it back to the database db_update('field_config_instance')->fields(array('data' => serialize($data)))->condition('field_name', $field)->condition('entity_type', $entity_type)->condition('bundle', $bundle)->execute(); } }
/** * Implements \SiteAudit\Check\Abstract\calculateScore(). */ public function calculateScore() { $this->registry['field_api_map'] = field_info_field_map(); $this->registry['field_instance_counts'] = array(); foreach ($this->registry['field_api_map'] as $field_name => $field) { foreach ($field['bundles'] as $entity_type => $bundle_names) { foreach ($bundle_names as $bundle_name) { $query = new EntityFieldQuery(); $query->entityCondition('entity_type', $entity_type)->entityCondition('bundle', $bundle_name)->fieldCondition($field_name)->count(); $field_count = $query->execute(); $this->registry['field_instance_counts'][$bundle_name][$entity_type][$field_name] = $field_count; } } } return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_INFO; }
/** * {@inheritdoc} */ public function isField($entity_type, $field_name) { $map = field_info_field_map(); return !empty($map[$field_name]) && array_key_exists($entity_type, $map[$field_name]['bundles']); }
/** * Implements \SiteAudit\Check\Abstract\calculateScore(). */ public function calculateScore() { $this->registry['field_api_map'] = field_info_field_map(); if (count($this->registry['field_api_map']) == 0) { $this->abort; return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_FAIL; } elseif (count($this->registry['field_api_map']) > 75) { return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_WARN; } return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_INFO; }