Exemplo n.º 1
0
/**
 * Forbid a field storage update from occurring.
 *
 * Any module may forbid any update for any reason. For example, the
 * field's storage module might forbid an update if it would change
 * the storage schema while data for the field exists. A field type
 * module might forbid an update if it would change existing data's
 * semantics, or if there are external dependencies on field settings
 * that cannot be updated.
 *
 * To forbid the update from occurring, throw a
 * \Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException.
 *
 * @param \Drupal\field\FieldStorageConfigInterface $field_storage
 *   The field storage as it will be post-update.
 * @param \Drupal\field\FieldStorageConfigInterface $prior_field_storage
 *   The field storage as it is pre-update.
 *
 * @see entity_crud
 */
function hook_field_storage_config_update_forbid(\Drupal\field\FieldStorageConfigInterface $field_storage, \Drupal\field\FieldStorageConfigInterface $prior_field_storage)
{
    if ($field_storage->module == 'options' && $field_storage->hasData()) {
        // Forbid any update that removes allowed values with actual data.
        $allowed_values = $field_storage->getSetting('allowed_values');
        $prior_allowed_values = $prior_field_storage->getSetting('allowed_values');
        $lost_keys = array_keys(array_diff_key($prior_allowed_values, $allowed_values));
        if (_options_values_in_use($field_storage->getTargetEntityTypeId(), $field_storage->getName(), $lost_keys)) {
            throw new \Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException(t('A list field (@field_name) with existing data cannot have its keys changed.', array('@field_name' => $field_storage->getName())));
        }
    }
}
 /**
  * {@inheritdoc}
  */
 public function validateForm(array &$form, FormStateInterface $form_state)
 {
     parent::validateForm($form, $form_state);
     $field_storage_definitions = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions($this->entity->getTargetEntityTypeId());
     // Validate field cardinality.
     if ($form_state->getValue('cardinality') === 'number' && !$form_state->getValue('cardinality_number')) {
         $form_state->setErrorByName('cardinality_number', $this->t('Number of values is required.'));
     } elseif (!$this->entity->isNew() && isset($field_storage_definitions[$this->entity->getName()]) && $form_state->getValue('cardinality') != FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
         // Get a count of entities that have a value in a delta higher than the
         // one selected. Deltas start with 0, so the selected value does not
         // need to be incremented.
         $entities_with_higher_delta = \Drupal::entityQuery($this->entity->getTargetEntityTypeId())->condition($this->entity->getName() . '.%delta', $form_state->getValue('cardinality'))->count()->execute();
         if ($entities_with_higher_delta) {
             $form_state->setErrorByName('cardinality_number', $this->formatPlural($entities_with_higher_delta, 'There is @count entity with @delta or more values in this field.', 'There are @count entities with @delta or more values in this field.', ['@delta' => $form_state->getValue('cardinality') + 1]));
         }
     }
 }
Exemplo n.º 3
0
/**
 * Alter the Views data for a single Field API field.
 *
 * This is called on all modules even if there is no hook_field_views_data()
 * implementation for the field, and therefore may be used to alter the
 * default data that views_field_default_views_data() supplies for the
 * field storage.
 *
 *  @param array $data
 *    The views data for the field storage. This has the same format as the
 *    return value of hook_views_data().
 *  @param \Drupal\field\FieldStorageConfigInterface $field_storage
 *    The field storage config entity.
 *
 * @see views_views_data()
 * @see hook_field_views_data()
 * @see hook_field_views_data_views_data_alter()
 */
function hook_field_views_data_alter(array &$data, \Drupal\field\FieldStorageConfigInterface $field_storage)
{
    $entity_type_id = $field_storage->getTargetEntityTypeId();
    $field_name = $field_storage->getName();
    $entity_type = \Drupal::entityManager()->getDefinition($entity_type_id);
    $pseudo_field_name = 'reverse_' . $field_name . '_' . $entity_type_id;
    $table_mapping = \Drupal::entityManager()->getStorage($entity_type_id)->getTableMapping();
    list($label) = views_entity_field_label($entity_type_id, $field_name);
    $data['file_managed'][$pseudo_field_name]['relationship'] = array('title' => t('@entity using @field', array('@entity' => $entity_type->getLabel(), '@field' => $label)), 'help' => t('Relate each @entity with a @field set to the image.', array('@entity' => $entity_type->getLabel(), '@field' => $label)), 'id' => 'entity_reverse', 'field_name' => $field_name, 'entity_type' => $entity_type_id, 'field table' => $table_mapping->getDedicatedDataTableName($field_storage), 'field field' => $field_name . '_target_id', 'base' => $entity_type->getBaseTable(), 'base field' => $entity_type->getKey('id'), 'label' => t('!field_name', array('!field_name' => $field_name)), 'join_extra' => array(0 => array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE)));
}