Example #1
1
  /**
   * Build all necessary things for child form (form state, etc.).
   *
   * @param \Drupal\Core\Entity\EntityFormInterface $controller
   *   Entity form controller for child form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Parent form state object.
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   Entity object.
   * @param string $operation
   *   Operation that is to be performed in inline form.
   * @param array $parents
   *   Entity form #parents.
   *
   * @return \Drupal\Core\Form\FormStateInterface
   *   Child form state object.
   */
  public static function buildChildFormState(EntityFormInterface $controller, FormStateInterface $form_state, EntityInterface $entity, $operation, $parents) {
    $child_form_state = new FormState();

    $child_form_state->addBuildInfo('callback_object', $controller);
    $child_form_state->addBuildInfo('base_form_id', $controller->getBaseFormID());
    $child_form_state->addBuildInfo('form_id', $controller->getFormID());
    $child_form_state->addBuildInfo('args', array());

    // Copy values to child form.
    $child_form_state->setCompleteForm($form_state->getCompleteForm());
    $child_form_state->setUserInput($form_state->getUserInput());

    // Filter out all submitted values that are not directly relevant for this
    // IEF. Otherwise they might mess things up.
    $form_state_values = $form_state->getValues();
    $form_state_values = static::extractArraySequence($form_state_values, $parents);

    $child_form_state->setValues($form_state_values);
    $child_form_state->setStorage($form_state->getStorage());
    $value = \Drupal::entityTypeManager()->getStorage('entity_form_display')->load($entity->getEntityTypeId() . '.' . $entity->bundle() . '.' . $operation);
    $child_form_state->set('form_display', $value);

    // Since some of the submit handlers are run, redirects need to be disabled.
    $child_form_state->disableRedirect();

    // When a form is rebuilt after Ajax processing, its #build_id and #action
    // should not change.
    // @see drupal_rebuild_form()
    $rebuild_info = $child_form_state->getRebuildInfo();
    $rebuild_info['copy']['#build_id'] = TRUE;
    $rebuild_info['copy']['#action'] = TRUE;
    $child_form_state->setRebuildInfo($rebuild_info);

    $child_form_state->set('inline_entity_form', $form_state->get('inline_entity_form'));
    $child_form_state->set('langcode', $entity->language()->getId());

    $child_form_state->set('field', $form_state->get('field'));
    $child_form_state->setTriggeringElement($form_state->getTriggeringElement());
    $child_form_state->setSubmitHandlers($form_state->getSubmitHandlers());

    return $child_form_state;
  }
 /**
  * {@inheritdoc}
  */
 public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor)
 {
     $settings = $editor->getSettings();
     $medium_editors = array();
     foreach (entity_load_multiple('medium_editor') as $medium_editor) {
         $medium_editors[$medium_editor->id()] = $medium_editor->label();
     }
     // Default editor
     $form['default_editor'] = array('#type' => 'select', '#title' => $this->t('Medium Editor'), '#options' => $medium_editors, '#default_value' => $settings['default_editor'], '#description' => $this->t('Select the default editor for the authorized roles. Editors can be configured at <a href="!url">Medium admin page</a>.', array('!url' => \Drupal::url('medium.admin'))), '#empty_option' => '- ' . $this->t('Select an editor') . ' -');
     // Roles editors
     $role_ids = array();
     if ($format_form = $form_state->getCompleteForm()) {
         if (isset($format_form['roles']['#value'])) {
             $role_ids = $format_form['roles']['#value'];
         } elseif (isset($format_form['roles']['#default_value'])) {
             $role_ids = $format_form['roles']['#default_value'];
         }
     } elseif ($format = $editor->getFilterFormat()) {
         $role_ids = array_keys(filter_get_roles_by_format($format));
     }
     if (count($role_ids) > 1) {
         $form['roles_editors'] = array('#type' => 'details', '#title' => t('Role specific editors'));
         $roles = user_roles();
         foreach ($role_ids as $role_id) {
             $form['roles_editors'][$role_id] = array('#type' => 'select', '#title' => $this->t('Editor for %role', array('%role' => $roles[$role_id]->label())), '#options' => $medium_editors, '#default_value' => isset($settings['roles_editors'][$role_id]) ? $settings['roles_editors'][$role_id] : '', '#empty_option' => '- ' . $this->t('Use the default') . ' -');
         }
     }
     return $form;
 }
  /**
   * {@inheritdoc}
   */
  public function getNewValues(FormStateInterface $form_state) {
    // If entity must have a prefix
    // (e.g. entity_form_mode, entity_view_mode, ...).
    $field_prefix = '';
    if (isset($form_state->getCompleteForm()['id']['#field_prefix'])) {
      $field_prefix = $form_state->getCompleteForm()['id']['#field_prefix'];
    }

    return [
      'id' => $field_prefix . $form_state->getValue('id'),
      'label' => $form_state->getValue('label'),
    ];
  }
Example #4
0
 /**
  * {@inheritdoc}
  */
 public function setCompleteForm(array &$complete_form)
 {
     $sub_form =& $this->applySubKeys($this->mainFormState->getCompleteForm());
     $sub_form = $complete_form;
     return $this;
 }
Example #5
0
 /**
  * Processes a machine-readable name form element.
  *
  * @param array $element
  *   The form element to process. See main class documentation for properties.
  * @param \Drupal\Core\Form\FormStateInterface $form_state
  *   The current state of the form.
  * @param array $complete_form
  *   The complete form structure.
  *
  * @return array
  *   The processed element.
  */
 public static function processMachineName(&$element, FormStateInterface $form_state, &$complete_form)
 {
     // We need to pass the langcode to the client.
     $language = \Drupal::languageManager()->getCurrentLanguage();
     // Apply default form element properties.
     $element += array('#title' => t('Machine-readable name'), '#description' => t('A unique machine-readable name. Can only contain lowercase letters, numbers, and underscores.'), '#machine_name' => array(), '#field_prefix' => '', '#field_suffix' => '', '#suffix' => '');
     // A form element that only wants to set one #machine_name property (usually
     // 'source' only) would leave all other properties undefined, if the defaults
     // were defined by an element plugin. Therefore, we apply the defaults here.
     $element['#machine_name'] += array('source' => array('label'), 'target' => '#' . $element['#id'], 'label' => t('Machine name'), 'replace_pattern' => '[^a-z0-9_]+', 'replace' => '_', 'standalone' => FALSE, 'field_prefix' => $element['#field_prefix'], 'field_suffix' => $element['#field_suffix']);
     // By default, machine names are restricted to Latin alphanumeric characters.
     // So, default to LTR directionality.
     if (!isset($element['#attributes'])) {
         $element['#attributes'] = array();
     }
     $element['#attributes'] += array('dir' => LanguageInterface::DIRECTION_LTR);
     // The source element defaults to array('name'), but may have been overridden.
     if (empty($element['#machine_name']['source'])) {
         return $element;
     }
     // Retrieve the form element containing the human-readable name from the
     // complete form in $form_state. By reference, because we may need to append
     // a #field_suffix that will hold the live preview.
     $key_exists = NULL;
     $source = NestedArray::getValue($form_state->getCompleteForm(), $element['#machine_name']['source'], $key_exists);
     if (!$key_exists) {
         return $element;
     }
     $suffix_id = $source['#id'] . '-machine-name-suffix';
     $element['#machine_name']['suffix'] = '#' . $suffix_id;
     if ($element['#machine_name']['standalone']) {
         $element['#suffix'] = $element['#suffix'] . ' <small id="' . $suffix_id . '">&nbsp;</small>';
     } else {
         // Append a field suffix to the source form element, which will contain
         // the live preview of the machine name.
         $source += array('#field_suffix' => '');
         $source['#field_suffix'] = $source['#field_suffix'] . ' <small id="' . $suffix_id . '">&nbsp;</small>';
         $parents = array_merge($element['#machine_name']['source'], array('#field_suffix'));
         NestedArray::setValue($form_state->getCompleteForm(), $parents, $source['#field_suffix']);
     }
     $element['#attached']['library'][] = 'core/drupal.machine-name';
     $element['#attached']['drupalSettings']['machineName']['#' . $source['#id']] = $element['#machine_name'];
     $element['#attached']['drupalSettings']['langcode'] = $language->getId();
     return $element;
 }
 /**
  * {@inheritdoc}
  */
 public function validate(array $form, FormStateInterface $form_state)
 {
     // Only validate on edit.
     if ($form_state->hasValue('keyed_styles')) {
         // Check if another breakpoint group is selected.
         if ($form_state->getValue('breakpoint_group') != $form_state->getCompleteForm()['breakpoint_group']['#default_value']) {
             // Remove the image style mappings since the breakpoint ID has changed.
             $form_state->unsetValue('keyed_styles');
         }
         // @todo Filter 'sizes_image_styles' to a normal array in
         // https://www.drupal.org/node/2334387. For an example see
         // \Drupal\Core\Block\BlockBase::validateConfigurationForm().
     }
 }
 /**
  * {@inheritdoc}
  */
 public function validate(array $form, FormStateInterface $form_state)
 {
     // Only validate on edit.
     if ($form_state->hasValue('keyed_mappings')) {
         // Check if another breakpoint group is selected.
         if ($form_state->getValue('breakpointGroup') != $form_state->getCompleteForm()['breakpointGroup']['#default_value']) {
             // Remove the mappings since the breakpoint ID has changed.
             $form_state->unsetValue('keyed_mappings');
         }
     }
 }
  /**
   * Build all necessary things for child form (form state, etc.).
   *
   * @param \Drupal\Core\Entity\EntityFormInterface $controller
   *   Entity form controller for child form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Parent form state object.
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   Entity object.
   * @param string $operation
   *   Operation that is to be performed in inline form.
   *
   * @return \Drupal\Core\Form\FormStateInterface
   *   Child form state object.
   */
  public static function buildChildFormState(EntityFormInterface $controller, FormStateInterface $form_state, EntityInterface $entity, $operation) {
    $child_form_state = new FormState();

    $child_form_state->addBuildInfo('callback_object', $controller);
    $child_form_state->addBuildInfo('base_form_id', $controller->getBaseFormID());
    $child_form_state->addBuildInfo('form_id', $controller->getFormID());
    $child_form_state->addBuildInfo('args', array());

    // Copy values to child form.
    $child_form_state->setCompleteForm($form_state->getCompleteForm());
    $child_form_state->setUserInput($form_state->getUserInput());
    $child_form_state->setValues($form_state->getValues());
    $child_form_state->setStorage($form_state->getStorage());
    $child_form_state->set('form_display', entity_get_form_display($entity->getEntityTypeId(), $entity->bundle(), $operation));

    // Since some of the submit handlers are run, redirects need to be disabled.
    $child_form_state->disableRedirect();

    // When a form is rebuilt after Ajax processing, its #build_id and #action
    // should not change.
    // @see drupal_rebuild_form()
    $rebuild_info = $child_form_state->getRebuildInfo();
    $rebuild_info['copy']['#build_id'] = TRUE;
    $rebuild_info['copy']['#action'] = TRUE;
    $child_form_state->setRebuildInfo($rebuild_info);

    $child_form_state->set('inline_entity_form', $form_state->get('inline_entity_form'));
    $child_form_state->set('langcode', $entity->language()->getId());

    $child_form_state->set('field', $form_state->get('field'));
    $child_form_state->setTriggeringElement($form_state->getTriggeringElement());
    $child_form_state->setSubmitHandlers($form_state->getSubmitHandlers());

    return $child_form_state;
  }
 /**
  * {@inheritdoc}
  */
 public function &getCompleteForm()
 {
     return $this->decoratedFormState->getCompleteForm();
 }