/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $base_fields = array_filter($form_state->getValue('base_fields')); $new_map = []; // Clone Base Fields foreach ($base_fields as $base_field) { /** @var \Drupal\field\Entity\FieldConfig $new_field */ $new_field = $this->fieldManager->cloneField($this->entity, $base_field); $new_map[$new_field->getName()] = $base_field; } // Clone Configurable Fields if ($config_field_ids = $form_state->getValue('config_fields')) { foreach ($config_field_ids as $config_field_id) { /** @var FieldConfig $config_definition */ if ($config_definition = FieldConfig::load($config_field_id)) { $field_name = $config_definition->getFieldStorageDefinition()->getName(); $new_field = $this->fieldManager->cloneField($this->entity, $field_name, $config_definition->id()); $new_map[$new_field->getName()] = $field_name; } } } if ($new_map) { // Update Map $new_map += $this->entity->getFieldMap(); $this->entity->setFieldMap($new_map); $this->entity->save(); drupal_set_message($this->t('The fields have been created and mapped.')); if ($this->currentUser()->hasPermission('administer scheduled_update form display')) { // Redirect to form display so user and adjust settings. $form_state->setRedirectUrl(Url::fromRoute("entity.entity_form_display.scheduled_update.default", array($this->entity->getEntityTypeId() => $this->entity->id()))); } else { drupal_set_message($this->t('You do not have permission to administer fields on Scheduled Updates.'), 'warning'); } } }
/** * Get the Runner Plugin Instance for a Type. * @param \Drupal\scheduled_updates\ScheduledUpdateTypeInterface $scheduledUpdateType * * @return null|object */ public function getUpdateRunnerInstance(ScheduledUpdateTypeInterface $scheduledUpdateType) { $runner_settings = $scheduledUpdateType->getUpdateRunnerSettings(); $runner_settings['updater_type'] = $scheduledUpdateType->id(); if ($this->runnerManager->hasDefinition($runner_settings['id'])) { return $this->runnerManager->createInstance($runner_settings['id'], $runner_settings); } return NULL; }
/** * @param \Drupal\scheduled_updates\ScheduledUpdateTypeInterface $updateType * * @return FieldConfig[] array */ protected function getSourceFields(ScheduledUpdateTypeInterface $updateType) { $source_fields = []; $fields = $this->FieldManager()->getFieldDefinitions('scheduled_update', $updateType->id()); foreach ($fields as $field_id => $field) { if (! $field instanceof BaseFieldDefinition) { $source_fields[$field_id] = $field; } } return $source_fields; }
public function addFormAccess(AccountInterface $account, ScheduledUpdateTypeInterface $scheduled_update_type) { if ($scheduled_update_type->isEmbeddedType()) { return AccessResult::forbidden(); } $type_id = $scheduled_update_type->id(); return AccessResult::allowedIfHasPermission($account, "create {$type_id} scheduled updates"); }
/** * Returns a list of scheduled updates permissions for a given scheduled udpate type. * * @param \Drupal\scheduled_updates\entity\ScheduledUpdateType|\Drupal\scheduled_updates\ScheduledUpdateTypeInterface $type * The node type. * * @return array An associative array of permission names and descriptions. * An associative array of permission names and descriptions. */ protected function buildPermissions(ScheduledUpdateTypeInterface $type) { $type_id = $type->id(); $type_params = array('%type_name' => $type->label()); return array("create {$type_id} scheduled updates" => array('title' => $this->t('%type_name: Create new scheduled updates', $type_params)), "edit own {$type_id} scheduled updates" => array('title' => $this->t('%type_name: Edit own scheduled updates', $type_params)), "edit any {$type_id} scheduled updates" => array('title' => $this->t('%type_name: Edit any scheduled updates', $type_params)), "delete own {$type_id} scheduled updates" => array('title' => $this->t('%type_name: Delete own scheduled updates', $type_params)), "delete any {$type_id} scheduled updates" => array('title' => $this->t('%type_name: Delete any scheduled updates', $type_params))); }
/** * {@inheritdoc} */ public function updateExistingReferenceFields(array $existing_field_settings, ScheduledUpdateTypeInterface $scheduled_update_type) { $fields = $existing_field_settings['fields']; foreach ($fields as $field_ids) { $field_ids = array_filter($field_ids); foreach ($field_ids as $field_id) { /** @var FieldConfig $field_config */ $field_config = FieldConfig::load($field_id); $settings = $field_config->getSetting('handler_settings'); $settings['target_bundles'][] = $scheduled_update_type->id(); $field_config->setSetting('handler_settings', $settings); $field_config->save(); } } }
/** * Create a form display for a newly clone field. * * This function attempts to use same setting settings as the source field. * * @param \Drupal\scheduled_updates\ScheduledUpdateTypeInterface $scheduled_update_type * @param $field_name * @param $field_config_id * @param $entity_type * @param $definition * Source field definition * @param $new_field_name */ protected function createFormDisplay(ScheduledUpdateTypeInterface $scheduled_update_type, $field_config_id, FieldStorageDefinitionInterface $definition, $new_field_name) { $destination_bundle = $scheduled_update_type->id(); $field_name = $definition->getName(); $entity_type = $scheduled_update_type->getUpdateEntityType(); /** @var EntityFormDisplay $destination_form_display */ $destination_form_display = EntityFormDisplay::load("scheduled_update.$destination_bundle.default"); if (empty($destination_form_display)) { $destination_form_display = EntityFormDisplay::create([ 'targetEntityType' => 'scheduled_update', 'bundle' => $destination_bundle, 'mode' => 'default', 'status' => TRUE, ]); } $display_options = []; if ($field_config_id) { $parts = explode('.', $field_config_id); $source_bundle = $parts[1]; /** @var EntityFormDisplay $source_form_display */ $source_form_display = EntityFormDisplay::load("$entity_type.$source_bundle.default"); $display_options = $source_form_display->getComponent($field_name); } else { if ($definition instanceof BaseFieldDefinition) { $display_options = $definition->getDisplayOptions('form'); if (empty($display_options)) { if ($definition->getType()) { // Provide default display for base boolean fields that don't have their own form display $display_options = [ 'type' => 'boolean_checkbox', 'settings' => ['display_label' => TRUE], ]; } } } } if (empty($display_options)) { $display_options = []; } if ($destination_form_display) { $destination_form_display->setComponent($new_field_name, $display_options); $destination_form_display->save(); } else { // Alert user if display options could not be created. // @todo Create default display options even none on source. drupal_set_message( $this->t( 'Form display options could not be created for @field they will have to be created manually.', ['@field' => $field_name] ), 'warning'); } }