/** * Get update runner for scheduled update types. * * @param array $update_types * Update ids to load runners for. If empty return all. * * @return \Drupal\scheduled_updates\Plugin\UpdateRunnerInterface[] */ protected function getUpdateTypeRunners(array $update_types = []) { $bundles = $this->entityTypeBundleInfo->getBundleInfo('scheduled_update'); /** @var UpdateRunnerInterface[] $runners */ $runners = []; foreach ($bundles as $bundle => $bundle_info) { if (empty($update_types) || in_array($bundle, $update_types)) { /** @var ScheduledUpdateType $updater */ $updater = ScheduledUpdateType::load($bundle); if ($runner = $this->getUpdateRunnerInstance($updater)) { $runners[$bundle] = $runner; } else { // @todo User logger for missing plugins /* drupal_set_message(t('Missing plugin in @plugin_id in update type @type', [ '@plugin_id' => $runner_settings['id'], '@type' => $updater->label(), ])); */ } } } return $runners; }
/** * Returns an array of node type permissions. * * @return array * The node type permissions. * @see \Drupal\user\PermissionHandlerInterface::getPermissions() */ public function scheduledUpdateTypesPermissions() { $perms = array(); // Generate scheduled_update permissions for all scheduled updates types. foreach (ScheduledUpdateType::loadMultiple() as $type) { $perms += $this->buildPermissions($type); } return $perms; }
/** * Add conditions to a query to select updates to run. * * @param \Drupal\Core\Entity\Query\QueryInterface $query * @param string $condition_prefix * String to attach to the beginning of all conditions if the base table is not updates. */ protected function addActiveUpdateConditions(QueryInterface $query, $condition_prefix = '') { $query->condition($condition_prefix. 'update_timestamp', REQUEST_TIME, '<='); $query->condition($condition_prefix. 'type', $this->scheduled_update_type->id()); $query->condition( $condition_prefix. 'status', [ ScheduledUpdateInterface::STATUS_UNRUN, // @todo How to handle requeued items. //ScheduledUpdateInterface::STATUS_REQUEUED, ], 'IN' ); }
/** * @param $runner_settings * * @return UpdateRunnerInterface */ protected function createRunnerInstance(&$runner_settings, FormStateInterface $form_state) { if (empty($runner_settings)) { $runner_settings = $this->entity->getUpdateRunnerSettings(); } if (!$this->runnerManager->hasDefinition($runner_settings['id'])) { // Settings is using plugin which no longer exists. $runner_settings = ['id' => 'default_embedded']; } /** @var UpdateRunnerInterface $update_runner */ $update_runner = $this->runnerManager->createInstance($runner_settings['id'], $runner_settings); $form_state->set('update_runner', $runner_settings); $form_state->set('scheduled_update_type', $this->entity); return $update_runner; }
/** * Clone a single field from the settings on type add form. * * This creates a default value for the field if chosen. * * @param $entity_type * @param $bundle * @param $clone_field */ protected function cloneSingleField($entity_type,$clone_field, FormStateInterface $form_state, $bundle = NULL) { $clone_field_id = NULL; $values = $form_state->getValues(); if (isset($values['default_value_input'])) { $default_value = [$values['default_value_input'][$clone_field]]; $hide = $values['default_value_input']['_no_form_display']; } else { $default_value = []; $hide = FALSE; } if ($bundle) { $clone_field_definition = $this->getFieldDefinition($entity_type, $bundle, $clone_field); if (!$clone_field_definition instanceof BaseFieldDefinition) { $clone_field_id = $clone_field_definition->id(); } } $cloned_field = $this->fieldManager->cloneField($this->entity, $clone_field, $clone_field_id, $default_value, $hide); $this->entity->setFieldMap([$cloned_field->getName() => $clone_field]); $this->entity->save(); }
protected function checkAddForm($type_id, $label, $fields, $only_type) { $logged_id_user = $this->loggedInUser; $add_user = $this->createUser(["create {$type_id} scheduled updates"]); $this->drupalLogin($add_user); $this->drupalGet('admin/content/scheduled-update/add'); if ($only_type) { // Form shown if only type. $this->checkFieldLabels($fields); } else { $this->assertText($label); /** @var ScheduledUpdateType[] $types */ $types = ScheduledUpdateType::loadMultiple(); // Check that all types are shown on the add page. foreach ($types as $type) { $this->assertText($type->label()); } } //$this->assertText($label); $this->drupalGet("admin/content/scheduled-update/add/{$type_id}"); $this->assertText(new FormattableMarkup('Create @label Scheduled Update', ['@label' => $label])); $this->checkFieldLabels($fields); $this->drupalLogin($logged_id_user); }
/** * BaseUpdateRunner constructor. * * @param array $configuration * @param string $plugin_id * @param mixed $plugin_definition * @param \Drupal\Core\Entity\EntityFieldManagerInterface $fieldManager * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager * @param \Drupal\scheduled_updates\UpdateUtils $updateUtils * @param \Drupal\Core\Session\AccountSwitcherInterface $accountSwitcher */ public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityFieldManagerInterface $fieldManager, EntityTypeManagerInterface $entityTypeManager, UpdateUtils $updateUtils, AccountSwitcherInterface $accountSwitcher) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->fieldManager = $fieldManager; $this->entityTypeManager = $entityTypeManager; $this->updateUtils = $updateUtils; $this->accountSwitcher = $accountSwitcher; if (!empty($configuration['updater_type'])) { $this->scheduled_update_type = ScheduledUpdateType::load($configuration['updater_type']); } }
/** * {@inheritdoc} */ public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) { /** @var ScheduledUpdateType $update_type */ if ($update_type = ScheduledUpdateType::load($bundle)) { $fields['entity_ids'] = clone $base_field_definitions['entity_ids']; if ($update_type->isIndependentType()) { // @todo Update other setting per bundle: cardinality, required, default display etc. // @todo Add reference field settings on Type edit page $fields['entity_ids']->setSetting('target_type', $update_type->getUpdateEntityType()); $fields['entity_ids']->setDisplayOptions('form', array('type' => 'entity_reference_autocomplete', 'weight' => -10)); } else { $fields['entity_ids']->setDisplayConfigurable('form', FALSE); } return $fields; } return array(); }
/** * 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} */ public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) { /** @var ScheduledUpdateType $update_type */ if ($update_type = ScheduledUpdateType::load($bundle)) { $fields['entity_ids'] = clone $base_field_definitions['entity_ids']; /** @var BaseFieldDefinition $definition */ $definition =& $fields['entity_ids']; if ($update_type->isIndependentType()) { // @todo Update other setting per bundle: cardinality, required, default display etc. // @todo Add reference field settings on Type edit page $definition->setSetting('target_type', $update_type->getUpdateEntityType()); $definition->setDisplayOptions('form', array( 'type' => 'entity_reference_autocomplete', 'weight' => -10, )); $runner_settings = $update_type->getUpdateRunnerSettings(); if (isset($runner_settings['bundles'])) { $bundles = array_filter($runner_settings['bundles']); $definition->setSetting('handler_settings', ['target_bundles' => $bundles]); } } else { $definition->setDisplayConfigurable('form', FALSE); $definition->setDisplayConfigurable('view', FALSE); } return $fields; } return array(); }