You are here

class ScheduledUpdateTypeAddAsFieldForm in Scheduled Updates 8

Class ScheduledUpdateTypeAddAsFieldForm.

@package Drupal\scheduled_updates\Form

Hierarchy

Expanded class hierarchy of ScheduledUpdateTypeAddAsFieldForm

File

src/Form/ScheduledUpdateTypeAddAsFieldForm.php, line 20
Contains \Drupal\scheduled_updates\Form\ScheduledUpdateTypeAddAsFieldForm.

Namespace

Drupal\scheduled_updates\Form
View source
class ScheduledUpdateTypeAddAsFieldForm extends ScheduledUpdateTypeBaseForm {

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);
    $form['update_entity_type'] = [
      '#type' => 'value',
      '#value' => $this
        ->getCurrentEntityType($form_state),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    $entity_type = $this
      ->getCurrentEntityType($form_state);
    $values = $form_state
      ->cleanValues()
      ->getValues();
    $bundle = $form_state
      ->get('target_bundle');
    $clone_field = $values['clone_field'];
    $clone_field_id = NULL;

    // When adding a field we don't expose the Label and Id of Bundle itself.
    $type_label = $this
      ->entityLabel($entity_type) . ' - ' . $this
      ->getFieldLabel($entity_type, $bundle, $clone_field);
    $this->entity
      ->set('label', $type_label);
    $this->entity
      ->set('id', $this
      ->createNewUpdateTypeName($entity_type, $clone_field));
    parent::doSave($form, $form_state);
    $this
      ->cloneSingleField($entity_type, $clone_field, $form_state, $bundle);
    $bundle_type = $this->entityTypeManager
      ->getDefinition($entity_type)
      ->getBundleEntityType();
    $form_state
      ->setRedirectUrl(Url::fromRoute("entity.entity_form_display.{$entity_type}.default", array(
      $bundle_type => $form_state
        ->get('target_bundle'),
    )));
  }
  public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL, $mode = 'independent', $bundle = '') {
    $form_state
      ->set('target_entity_type_id', $entity_type_id);
    $form_state
      ->set('target_bundle', $bundle);
    $this->entity
      ->setUpdateEntityType($entity_type_id);
    return parent::buildForm($form, $form_state);
  }
  public function afterBuild(array $form, FormStateInterface $form_state) {
    $form = parent::afterBuild($form, $form_state);
    $trigger = $form_state
      ->getTriggeringElement();
    if ($trigger['#name'] == 'clone_field' && ($clone_field = $form_state
      ->getValue('clone_field'))) {
      $new_field_element =& $form['type_dependent_elements']['reference_settings']['new_field'];
      $selected_field_label = $form['type_dependent_elements']['clone_field']['#options'][$clone_field];
      $new_field_element['label']['#value'] = $selected_field_label . ' ' . $this
        ->t('Update');
      $new_field_element['field_name']['#value'] = '';
    }
    return $form;
  }

  /**
   * Create an update type name programmatically.
   *
   * @param $entity_type
   * @param $clone_field
   *
   * @return string
   */
  protected function createNewUpdateTypeName($entity_type, $clone_field) {
    $name = $entity_type . '__' . $clone_field;
    $suffix = 0;
    $new_name = $name;
    while (ScheduledUpdateType::load($new_name)) {
      $suffix++;
      $new_name = $name . '_' . $suffix;
    }
    return $new_name;
  }

  /**
   * {@inheritdoc}
   *
   * Override to only return runners that support embedded updates.
   */
  protected function getSupportedRunnerDefinitions() {
    $definitions = parent::getSupportedRunnerDefinitions();
    $supported_definitions = [];
    foreach ($definitions as $id => $definition) {
      if (in_array('embedded', $definition['update_types'])) {
        $supported_definitions[$id] = $definition;
      }
    }
    return $supported_definitions;
  }

  /**
   *{@inheritdoc}
   */
  protected function getDefaultBundle($field_selected, FormStateInterface $form_state) {
    return $form_state
      ->get('target_bundle');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ClassUtilsTrait::bundleLabel protected function
ClassUtilsTrait::definitionClassImplementsInterface protected function Determines if the class for an entity type definition implements and interface.
ClassUtilsTrait::entityLabel protected function
ClassUtilsTrait::entityTypeManager protected function
ClassUtilsTrait::getEntityOwner protected function Get the entity owner if applicable.
ClassUtilsTrait::getRevisionOwner protected function Get the revision owner for an ContentEntity.
ClassUtilsTrait::implementsInterface protected function Determines if an object or class name implements any interfaces in a list.
ClassUtilsTrait::revisionOwnerInterfaces protected function Get class names of interfaces that support revision ownership.
ClassUtilsTrait::targetSupportBundles protected function
ClassUtilsTrait::targetTypeBundleLabel protected function
ClassUtilsTrait::targetTypeLabel public function
ClassUtilsTrait::typeSupportsBundles protected function
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EntityForm::$entityTypeManager protected property The entity type manager. 3
EntityForm::$operation protected property The name of the current operation.
EntityForm::$privateEntityManager private property The entity manager.
EntityForm::actions protected function Returns an array of supported actions for the current entity form. 29
EntityForm::actionsElement protected function Returns the action form element for the current entity form.
EntityForm::buildEntity public function Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface::buildEntity 2
EntityForm::copyFormValuesToEntity protected function Copies top-level form values to entity properties 7
EntityForm::getBaseFormId public function Returns a string identifying the base form. Overrides BaseFormIdInterface::getBaseFormId 5
EntityForm::getEntity public function Gets the form entity. Overrides EntityFormInterface::getEntity
EntityForm::getEntityFromRouteMatch public function Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface::getEntityFromRouteMatch 1
EntityForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId 10
EntityForm::getOperation public function Gets the operation identifying the form. Overrides EntityFormInterface::getOperation
EntityForm::init protected function Initialize the form state and the entity before the first form build. 3
EntityForm::prepareEntity protected function Prepares the entity object before the form is built first. 3
EntityForm::prepareInvokeAll protected function Invokes the specified prepare hook variant.
EntityForm::processForm public function Process callback: assigns weights and hides extra fields.
EntityForm::setEntity public function Sets the form entity. Overrides EntityFormInterface::setEntity
EntityForm::setEntityManager public function Sets the entity manager for this form. Overrides EntityFormInterface::setEntityManager
EntityForm::setEntityTypeManager public function Sets the entity type manager for this form. Overrides EntityFormInterface::setEntityTypeManager
EntityForm::setModuleHandler public function Sets the module handler for this form. Overrides EntityFormInterface::setModuleHandler
EntityForm::setOperation public function Sets the operation for this form. Overrides EntityFormInterface::setOperation
EntityForm::submitForm public function This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state… Overrides FormInterface::submitForm 17
EntityForm::__get public function
EntityForm::__set public function
FieldUtilsTrait::getBundleDestinationOptions protected function
FieldUtilsTrait::getDestinationFields protected function Return all fields that can be used as destinations fields.
FieldUtilsTrait::getDestinationFieldsOptions protected function
FieldUtilsTrait::getEntityDestinationOptions protected function
FieldUtilsTrait::getFieldDefinition public function Get field definition on bundle.
FieldUtilsTrait::getFieldLabel protected function
FieldUtilsTrait::getFieldStorageDefinition protected function Utility Function to load a single field definition.
FieldUtilsTrait::getMatchingFieldTypes protected function Get Fields that can used as a destination field for this type.
FieldUtilsTrait::getSourceFields protected function
FieldUtilsTrait::isDestinationFieldCompatible protected function Check if a field on the entity type to update is a possible destination field.
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
ScheduledUpdateTypeAddAsFieldForm::afterBuild public function Form element #after_build callback: Updates the entity with submitted data. Overrides EntityForm::afterBuild
ScheduledUpdateTypeAddAsFieldForm::buildForm public function Form constructor. Overrides EntityForm::buildForm
ScheduledUpdateTypeAddAsFieldForm::createNewUpdateTypeName protected function Create an update type name programmatically.
ScheduledUpdateTypeAddAsFieldForm::form public function Gets the actual form array to be built. Overrides ScheduledUpdateTypeBaseForm::form
ScheduledUpdateTypeAddAsFieldForm::getDefaultBundle protected function Determine the default that should be used to create default value elements. Overrides ScheduledUpdateTypeBaseForm::getDefaultBundle
ScheduledUpdateTypeAddAsFieldForm::getSupportedRunnerDefinitions protected function Override to only return runners that support embedded updates. Overrides ScheduledUpdateTypeBaseForm::getSupportedRunnerDefinitions
ScheduledUpdateTypeAddAsFieldForm::save public function Form submission handler for the 'save' action. Overrides EntityForm::save
ScheduledUpdateTypeBaseForm::$entity protected property @var ScheduledUpdateType Overrides EntityForm::$entity
ScheduledUpdateTypeBaseForm::$entityFieldManager protected property
ScheduledUpdateTypeBaseForm::$fieldManager protected property
ScheduledUpdateTypeBaseForm::$moduleHandler protected property Overrides EntityForm::$moduleHandler
ScheduledUpdateTypeBaseForm::$runnerManager protected property
ScheduledUpdateTypeBaseForm::$updateUtils protected property
ScheduledUpdateTypeBaseForm::cloneSingleField protected function Clone a single field from the settings on type add form.
ScheduledUpdateTypeBaseForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
ScheduledUpdateTypeBaseForm::createCloneFieldSelect protected function Create an option to select one field to clone on type add form. 1
ScheduledUpdateTypeBaseForm::createDefaultValueElements protected function Create the default value elements for a field.
ScheduledUpdateTypeBaseForm::createNewFieldsElements protected function Create options for create a new entity reference field.
ScheduledUpdateTypeBaseForm::createRunnerElements protected function Create form elements for runner selection and options.
ScheduledUpdateTypeBaseForm::createRunnerInstance protected function Create an instance of the Runner plugin to be used with this Update Type.
ScheduledUpdateTypeBaseForm::doSave public function Save the entity on submit.
ScheduledUpdateTypeBaseForm::existingReferenceFields protected function Get existing entity reference field on target entity type that reference scheduled updates.
ScheduledUpdateTypeBaseForm::FieldManager public function Overrides FieldUtilsTrait::FieldManager
ScheduledUpdateTypeBaseForm::fieldNameExists public function Checks if a field machine name is taken.
ScheduledUpdateTypeBaseForm::getCurrentEntityType protected function Get the current entity type to be updated for update type.
ScheduledUpdateTypeBaseForm::getWidgetOverride protected function Get the widget that should used for the default value.
ScheduledUpdateTypeBaseForm::isDefaultCompatible private function Determine if we should try to make a default value widget.
ScheduledUpdateTypeBaseForm::runnerSupportsEmbedded protected function
ScheduledUpdateTypeBaseForm::setUpFieldReferences protected function Setup entity reference field for this update type on add.
ScheduledUpdateTypeBaseForm::typeDependentAjax protected function
ScheduledUpdateTypeBaseForm::updateRunnerSettings public function Ajax Form call back for Update Runner Fieldset.
ScheduledUpdateTypeBaseForm::updateTypeDependentSet public function Ajax Form call back for Create Reference Fieldset.
ScheduledUpdateTypeBaseForm::validateForm public function Form validation handler. Overrides FormBase::validateForm 1
ScheduledUpdateTypeBaseForm::__construct public function Constructs a ScheduledUpdateTypeForm object.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.