Ejemplo n.º 1
0
 /**
  * Returns the entity_form_display object used to build an entity form.
  *
  * Depending on the configuration of the form mode for the entity bundle, this
  * can be either the display object associated with the form mode, or the
  * 'default' display.
  *
  * This method should only be used internally when rendering an entity form.
  * When assigning suggested display options for a component in a given form
  * mode, entity_get_form_display() should be used instead, in order to avoid
  * inadvertently modifying the output of other form modes that might happen to
  * use the 'default' display too. Those options will then be effectively
  * applied only if the form mode is configured to use them.
  *
  * hook_entity_form_display_alter() is invoked on each display, allowing 3rd
  * party code to alter the display options held in the display before they are
  * used to generate render arrays.
  *
  * @param \Drupal\Core\Entity\FieldableEntityInterface $entity
  *   The entity for which the form is being built.
  * @param string $form_mode
  *   The form mode.
  *
  * @return \Drupal\Core\Entity\Display\EntityFormDisplayInterface
  *   The display object that should be used to build the entity form.
  *
  * @see entity_get_form_display()
  * @see hook_entity_form_display_alter()
  */
 public static function collectRenderDisplay(FieldableEntityInterface $entity, $form_mode)
 {
     $entity_type = $entity->getEntityTypeId();
     $bundle = $entity->bundle();
     // Check the existence and status of:
     // - the display for the form mode,
     // - the 'default' display.
     if ($form_mode != 'default') {
         $candidate_ids[] = $entity_type . '.' . $bundle . '.' . $form_mode;
     }
     $candidate_ids[] = $entity_type . '.' . $bundle . '.default';
     $results = \Drupal::entityQuery('entity_form_display')->condition('id', $candidate_ids)->condition('status', TRUE)->execute();
     // Load the first valid candidate display, if any.
     $storage = \Drupal::entityManager()->getStorage('entity_form_display');
     foreach ($candidate_ids as $candidate_id) {
         if (isset($results[$candidate_id])) {
             $display = $storage->load($candidate_id);
             break;
         }
     }
     // Else create a fresh runtime object.
     if (empty($display)) {
         $display = $storage->create(array('targetEntityType' => $entity_type, 'bundle' => $bundle, 'mode' => $form_mode, 'status' => TRUE));
     }
     // Let the display know which form mode was originally requested.
     $display->originalMode = $form_mode;
     // Let modules alter the display.
     $display_context = array('entity_type' => $entity_type, 'bundle' => $bundle, 'form_mode' => $form_mode);
     \Drupal::moduleHandler()->alter('entity_form_display', $display, $display_context);
     return $display;
 }
Ejemplo n.º 2
0
/**
 * Provide the allowed values for a 'list_*' field.
 *
 * Callback for options_allowed_values().
 *
 * 'list_*' fields can specify a callback to define the set of their allowed
 * values using the 'allowed_values_function' storage setting.
 *
 * That function will be called:
 *  - either in the context of a specific entity, which is then provided as the
 *    $entity parameter,
 *  - or for the field generally without the context of any specific entity or
 *    entity bundle (typically, Views needing a list of values for an exposed
 *    filter), in which case the $entity parameter is NULL.
 * This lets the callback restrict the set of allowed values or adjust the
 * labels depending on some conditions on the containing entity.
 *
 * For consistency, the set of values returned when an $entity is provided
 * should be a subset of the values returned when no $entity is provided.
 *
 * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $definition
 *   The field storage definition.
 * @param \Drupal\Core\Entity\FieldableEntityInterface|null $entity
 *   (optional) The entity context if known, or NULL if the allowed values are
 *   being collected without the context of a specific entity.
 * @param bool &$cacheable
 *   (optional) If an $entity is provided, the $cacheable parameter should be
 *   modified by reference and set to FALSE if the set of allowed values
 *   returned was specifically adjusted for that entity and cannot not be reused
 *   for other entities. Defaults to TRUE.
 *
 * @return array
 *   The array of allowed values. Keys of the array are the raw stored values
 *   (number or text), values of the array are the display labels. If $entity
 *   is NULL, you should return the list of all the possible allowed values in
 *   any context so that other code (e.g. Views filters) can support the allowed
 *   values for all possible entities and bundles.
 *
 * @ingroup callbacks
 * @see options_allowed_values()
 * @see options_test_allowed_values_callback()
 * @see options_test_dynamic_values_callback()
 */
function callback_allowed_values_function(FieldStorageDefinitionInterface $definition, FieldableEntityInterface $entity = NULL, &$cacheable = TRUE)
{
    if (isset($entity) && $entity->bundle() == 'not_a_programmer') {
        $values = array(1 => 'One', 2 => 'Two');
    } else {
        $values = array('Group 1' => array(0 => 'Zero', 1 => 'One'), 'Group 2' => array(2 => 'Two'));
    }
    return $values;
}
 /**
  * Returns the display object used to render an entity.
  *
  * See the collectRenderDisplays() method for details.
  *
  * @param \Drupal\Core\Entity\FieldableEntityInterface $entity
  *   The entity being rendered.
  * @param string $view_mode
  *   The view mode.
  *
  * @return \Drupal\Core\Entity\Display\EntityViewDisplayInterface
  *   The display object that should be used to render the entity.
  *
  * @see \Drupal\entity\Entity\EntityDisplay::collectRenderDisplays()
  */
 public static function collectRenderDisplay(FieldableEntityInterface $entity, $view_mode)
 {
     $displays = static::collectRenderDisplays(array($entity), $view_mode);
     return $displays[$entity->bundle()];
 }