/**
  * Form builder callback.
  *
  * @todo I don't know why this needs to be separate from the form() method.
  * It was in the form_alter version but we should see if we can just fold
  * it into the method above.
  *
  * @param $entity_type
  * @param \Drupal\Core\Config\Entity\ConfigEntityInterface $bundle
  * @param array $form
  * @param \Drupal\Core\Form\FormStateInterface $form_state
  */
 public function formBuilderCallback($entity_type, ConfigEntityInterface $bundle, &$form, FormStateInterface $form_state)
 {
     // @todo write a test for this.
     $bundle->setThirdPartySetting('workbench_moderation', 'enabled', $form_state->getValue('enable_moderation_state'));
     $bundle->setThirdPartySetting('workbench_moderation', 'allowed_moderation_states', array_keys(array_filter($form_state->getValue('allowed_moderation_states'))));
     $bundle->setThirdPartySetting('workbench_moderation', 'default_moderation_state', $form_state->getValue('default_moderation_state'));
 }
Exemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b)
 {
     if ($a->isLocked() == $b->isLocked()) {
         $a_label = $a->label();
         $b_label = $b->label();
         return strnatcasecmp($a_label, $b_label);
     }
     return $a->isLocked() ? 1 : -1;
 }
Exemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b)
 {
     /** @var \Drupal\Core\Entity\EntityDisplayModeInterface $a */
     /** @var \Drupal\Core\Entity\EntityDisplayModeInterface $b */
     // Sort by the type of entity the view mode is used for.
     $a_type = $a->getTargetType();
     $b_type = $b->getTargetType();
     $type_order = strnatcasecmp($a_type, $b_type);
     return $type_order != 0 ? $type_order : parent::sort($a, $b);
 }
Exemplo n.º 4
0
 /**
  * {@inheritdoc}
  */
 protected function getConfigNamesToDelete(ConfigEntityInterface $entity)
 {
     /** @var \Drupal\field\FieldStorageConfigInterface $field_storage */
     $field_storage = $entity->getFieldStorageDefinition();
     $config_names = [$entity->getConfigDependencyName()];
     // If there is only one bundle left for this field storage, it will be
     // deleted too, notify the user about dependencies.
     if (count($field_storage->getBundles()) <= 1) {
         $config_names[] = $field_storage->getConfigDependencyName();
     }
     return $config_names;
 }
 /**
  * Asserts that a config entity can be imported without changing it.
  *
  * @param \Drupal\Core\Config\Entity\ConfigEntityInterface $entity
  *   The config entity to test importing.
  */
 public function assertConfigEntityImport(ConfigEntityInterface $entity)
 {
     // Save original config information.
     $entity_uuid = $entity->uuid();
     $entity_type_id = $entity->getEntityTypeId();
     $original_data = $entity->toArray();
     // Copy everything to sync.
     $this->copyConfig(\Drupal::service('config.storage'), \Drupal::service('config.storage.sync'));
     // Delete the configuration from active. Don't worry about side effects of
     // deleting config like fields cleaning up field storages. The coming import
     // should recreate everything as necessary.
     $entity->delete();
     $this->configImporter()->reset()->import();
     $imported_entity = \Drupal::entityManager()->loadEntityByUuid($entity_type_id, $entity_uuid);
     $this->assertIdentical($original_data, $imported_entity->toArray());
 }
 /**
  * Tests a tracker with a dependency that gets removed.
  *
  * @param bool $remove_dependency
  *   Whether to remove the dependency from the tracker when the object
  *   depended on is deleted.
  *
  * @dataProvider dependencyTestDataProvider
  */
 public function testTrackerDependency($remove_dependency)
 {
     // Set the tracker for the index and save it. The tracker configuration
     // contains the dependencies it will return – in our case, we use the test
     // server.
     $dependency_key = $this->dependency->getConfigDependencyKey();
     $dependency_name = $this->dependency->getConfigDependencyName();
     $tracker = \Drupal::getContainer()->get('plugin.manager.search_api.tracker')->createInstance('search_api_test_dependencies', array($dependency_key => array($dependency_name)));
     $this->index->setTracker($tracker);
     $this->index->save();
     // Check the dependencies were calculated correctly.
     $dependencies = $this->index->getDependencies();
     $this->assertContains($dependency_name, $dependencies[$dependency_key], 'Tracker dependency correctly inserted');
     // Set our magic state key to let the test plugin know whether the
     // dependency should be removed or not. See
     // \Drupal\search_api_test_dependencies\Plugin\search_api\tracker\TestTracker::onDependencyRemoval().
     $key = 'search_api_test_dependencies.tracker.remove';
     \Drupal::state()->set($key, $remove_dependency);
     // If the index resets the tracker, it needs to know the ID of the default
     // tracker.
     if (!$remove_dependency) {
         \Drupal::configFactory()->getEditable('search_api.settings')->set('default_tracker', 'default')->save();
     }
     // Delete the tracker's dependency.
     $this->dependency->delete();
     // Reload the index and check it's still there.
     $this->reloadIndex();
     $this->assertInstanceOf('Drupal\\search_api\\IndexInterface', $this->index, 'Index not removed');
     // Make sure the dependency has been removed, one way or the other.
     $dependencies = $this->index->getDependencies();
     $dependencies += array($dependency_key => array());
     $this->assertNotContains($dependency_name, $dependencies[$dependency_key], 'Tracker dependency removed from index');
     // Depending on whether the plugin should have removed the dependency or
     // not, make sure the right action was taken.
     $tracker_instance = $this->index->getTrackerInstance();
     $tracker_id = $tracker_instance->getPluginId();
     $tracker_config = $tracker_instance->getConfiguration();
     if ($remove_dependency) {
         $this->assertEquals('search_api_test_dependencies', $tracker_id, 'Tracker not reset');
         $this->assertEmpty($tracker_config, 'Tracker settings adapted');
     } else {
         $this->assertEquals('default', $tracker_id, 'Tracker was reset');
         $this->assertEmpty($tracker_config, 'Tracker settings were cleared');
     }
 }
Exemplo n.º 7
0
 /**
  * {@inheritdoc}
  */
 public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b)
 {
     /** @var $a \Drupal\search\SearchPageInterface */
     /** @var $b \Drupal\search\SearchPageInterface */
     $a_status = (int) $a->status();
     $b_status = (int) $b->status();
     if ($a_status != $b_status) {
         return $a_status > $b_status ? -1 : 1;
     }
     return parent::sort($a, $b);
 }
Exemplo n.º 8
0
 /**
  * {@inheritdoc}
  */
 public function updateFromStorageRecord(ConfigEntityInterface $entity, array $values)
 {
     $entity->original = clone $entity;
     $data = $this->mapFromStorageRecords(array($values));
     $updated_entity = current($data);
     foreach (array_keys($values) as $property) {
         $value = $updated_entity->get($property);
         $entity->set($property, $value);
     }
     return $entity;
 }
Exemplo n.º 9
0
 /**
  * Adds the configured context definitions from the config entity.
  *
  * Example: for a reaction rule config all context definitions of the event
  * will be added.
  *
  * @param \Drupal\Core\Config\Entity\ConfigEntityInterface $rules_config
  *   The config entity.
  *
  * @return $this
  */
 public function addContextDefinitionsFrom(ConfigEntityInterface $rules_config)
 {
     if ($rules_config instanceof ReactionRuleConfig) {
         $event_name = $rules_config->getEvent();
         // @todo Use setter injection for the service.
         $event_definition = \Drupal::service('plugin.manager.rules_event')->getDefinition($event_name);
         foreach ($event_definition['context'] as $context_name => $context_definition) {
             $this->addContextDefinition($context_name, $context_definition);
         }
     }
     return $this;
 }
 /**
  * {@inheritdoc}
  */
 public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b)
 {
     // Sort the entities using the entity class's sort() method.
     // See \Drupal\Core\Config\Entity\ConfigEntityBase::sort().
     /* @var $a WorkflowTransitionInterface */
     /* @var $b WorkflowTransitionInterface */
     if (!$a->getFromSid() || !$b->getFromSid()) {
         return 0;
     }
     // First sort on From-State.
     $from_state_a = $a->getFromState();
     $from_state_b = $b->getFromState();
     if ($from_state_a->weight < $from_state_b->weight) {
         return -1;
     }
     if ($from_state_a->weight > $from_state_b->weight) {
         return +1;
     }
     // Then sort on To-State.
     $to_state_a = $a->getToState();
     $to_state_b = $b->getToState();
     if ($to_state_a->weight < $to_state_b->weight) {
         return -1;
     }
     if ($to_state_a->weight > $to_state_b->weight) {
         return +1;
     }
     return 0;
 }
Exemplo n.º 11
0
 /**
  * Sorts active blocks by weight; sorts inactive blocks by name.
  */
 public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b)
 {
     // Separate enabled from disabled.
     $status = $b->get('status') - $a->get('status');
     if ($status) {
         return $status;
     }
     // Sort by weight, unless disabled.
     if ($a->get('region') != static::BLOCK_REGION_NONE) {
         $weight = $a->get('weight') - $b->get('weight');
         if ($weight) {
             return $weight;
         }
     }
     // Sort by label.
     return strcmp($a->label(), $b->label());
 }
Exemplo n.º 12
0
 /**
  * {@inheritdoc}
  */
 public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b)
 {
     $a_status = (int) $a->status();
     $b_status = (int) $b->status();
     if ($a_status != $b_status) {
         return $a_status > $b_status ? -1 : 1;
     }
     return parent::sort($a, $b);
 }
Exemplo n.º 13
0
 /**
  * Returns config names to delete for the deletion confirmation form.
  *
  * @param \Drupal\Core\Config\Entity\ConfigEntityInterface $entity
  *   The entity being deleted.
  *
  * @return string[]
  *   A list of configuration names that will be deleted by this form.
  */
 protected function getConfigNamesToDelete(ConfigEntityInterface $entity)
 {
     return [$entity->getConfigDependencyName()];
 }
Exemplo n.º 14
0
 /**
  * @covers ::save
  * @covers ::doSave
  *
  * @depends testSaveInsert
  */
 public function testSaveRename(ConfigEntityInterface $entity)
 {
     $config_object = $this->getMockBuilder('Drupal\\Core\\Config\\Config')->disableOriginalConstructor()->getMock();
     $config_object->expects($this->atLeastOnce())->method('isNew')->will($this->returnValue(FALSE));
     $config_object->expects($this->exactly(1))->method('setData');
     $config_object->expects($this->once())->method('save');
     $config_object->expects($this->atLeastOnce())->method('get')->willReturn([]);
     $this->cacheTagsInvalidator->expects($this->once())->method('invalidateTags')->with(array($this->entityTypeId . '_list'));
     $this->configFactory->expects($this->once())->method('rename')->willReturn($this->configFactory);
     $this->configFactory->expects($this->exactly(1))->method('getEditable')->with('the_config_prefix.bar')->will($this->returnValue($config_object));
     $this->configFactory->expects($this->exactly(2))->method('loadMultiple')->with(array('the_config_prefix.foo'))->will($this->returnValue(array()));
     $this->configFactory->expects($this->once())->method('get')->with('the_config_prefix.foo')->will($this->returnValue($config_object));
     // Performing a rename does not change the original ID until saving.
     $this->assertSame('foo', $entity->getOriginalId());
     $entity->set('id', 'bar');
     $this->assertSame('foo', $entity->getOriginalId());
     $this->entityQuery->expects($this->once())->method('condition')->with('uuid', 'bar')->will($this->returnSelf());
     $this->entityQuery->expects($this->once())->method('execute')->will($this->returnValue(array($entity->id())));
     $return = $this->entityStorage->save($entity);
     $this->assertSame(SAVED_UPDATED, $return);
     $this->assertSame('bar', $entity->getOriginalId());
 }
Exemplo n.º 15
0
 /**
  * Builds a table row for overview form.
  *
  * @param \Drupal\Core\Config\Entity\ConfigEntityInterface $entity
  *   Data needed to build the list row.
  *
  * @return array
  *   A single table row for the overview.
  */
 public function overviewRow(ConfigEntityInterface $entity)
 {
     $label = $entity->label() ?: $this->t('@type: @id', array('@type' => $entity->getEntityTypeId(), '@id' => $entity->id()));
     // Get current job items for the entity to determine translation statuses.
     $source_lang = $entity->language()->getId();
     $current_job_items = tmgmt_job_item_load_latest('config', $entity->getEntityTypeId(), $entity->getConfigDependencyName(), $source_lang);
     $row['id'] = $entity->id();
     $definition = \Drupal::entityTypeManager()->getDefinition($entity->bundle());
     $row['config_id'] = $definition->getConfigPrefix() . '.' . $entity->id();
     if ($entity->hasLinkTemplate('edit-form')) {
         $row['title'] = $entity->toLink($label, 'edit-form');
     } else {
         // If the entity doesn't have a link we display a label.
         $row['title'] = $label;
     }
     // Load entity translation specific data.
     foreach (\Drupal::languageManager()->getLanguages() as $langcode => $language) {
         $translation_status = 'current';
         if ($langcode == $source_lang) {
             $translation_status = 'original';
         } elseif (!$this->isTranslated($langcode, $entity->getConfigDependencyName())) {
             $translation_status = 'missing';
         }
         // @todo Find a way to support marking configuration translations as outdated.
         $build = $this->buildTranslationStatus($translation_status, isset($current_job_items[$langcode]) ? $current_job_items[$langcode] : NULL);
         $row['langcode-' . $langcode] = ['data' => \Drupal::service('renderer')->render($build), 'class' => array('langstatus-' . $langcode)];
     }
     return $row;
 }
Exemplo n.º 16
0
 /**
  * {@inheritdoc}
  */
 public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b)
 {
     /* @var $a WorkflowState */
     /* @var $b WorkflowState */
     $a_wid = $a->wid;
     $b_wid = $b->wid;
     if ($a_wid == $b_wid) {
         $a_weight = $a->getWeight();
         $b_weight = $b->getWeight();
         return $a_weight < $b_weight ? -1 : 1;
     }
     return $a_wid < $b_wid ? -1 : 1;
 }
Exemplo n.º 17
0
 /**
  * Sorts active blocks by weight; sorts inactive blocks by name.
  */
 public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b)
 {
     // Separate enabled from disabled.
     $status = (int) $b->status() - (int) $a->status();
     if ($status !== 0) {
         return $status;
     }
     // Sort by weight, unless disabled.
     if ($a->getRegion() != static::BLOCK_REGION_NONE) {
         $weight = $a->getWeight() - $b->getWeight();
         if ($weight) {
             return $weight;
         }
     }
     // Sort by label.
     return strcmp($a->label(), $b->label());
 }
Exemplo n.º 18
0
 /**
  * Creates an event entity.
  *
  * @param \Drupal\Core\Config\Entity\ConfigEntityInterface
  *   An entity type.
  * @param array $settings
  *   Additional settings for the new entity.
  *
  * @return \Drupal\Core\Entity\EntityInterface
  *   An event.
  */
 function createEntity(ConfigEntityInterface $entity_type, $settings = [])
 {
     // @todo change to custom entity
     $entity = $this->drupalCreateNode(['type' => $entity_type->id()] + $settings);
     return $entity;
 }
Exemplo n.º 19
0
 /**
  * {@inheritdoc}
  */
 public function getBaseRouteParameters()
 {
     return array($this->entityType => $this->entity->id());
 }
Exemplo n.º 20
0
 /**
  * {@inheritdoc}
  */
 public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b)
 {
     /** @var \Drupal\system\ActionConfigEntityInterface $a */
     /** @var \Drupal\system\ActionConfigEntityInterface $b */
     $a_type = $a->getType();
     $b_type = $b->getType();
     if ($a_type != $b_type) {
         return strnatcasecmp($a_type, $b_type);
     }
     return parent::sort($a, $b);
 }
Exemplo n.º 21
0
 /**
  * Calls an entity's onDependencyRemoval() method.
  *
  * A helper method to call onDependencyRemoval() with the correct list of
  * affected entities. This list should only contain dependencies on the
  * entity. Configuration and content entity dependencies will be converted
  * into entity objects.
  *
  * @param \Drupal\Core\Config\Entity\ConfigEntityInterface $entity
  *   The entity to call onDependencyRemoval() on.
  * @param \Drupal\Core\Config\Entity\ConfigEntityInterface[] $dependent_entities
  *   The list of dependent configuration entities.
  * @param string $type
  *   The type of dependency being checked. Either 'module', 'theme', 'config'
  *   or 'content'.
  * @param array $names
  *   The specific names to check. If $type equals 'module' or 'theme' then it
  *   should be a list of module names or theme names. In the case of 'config'
  *   or 'content' it should be a list of configuration dependency names.
  *
  * @return bool
  *   TRUE if the entity has changed as a result of calling the
  *   onDependencyRemoval() method, FALSE if not.
  */
 protected function callOnDependencyRemoval(ConfigEntityInterface $entity, array $dependent_entities, $type, array $names)
 {
     $entity_dependencies = $entity->getDependencies();
     if (empty($entity_dependencies)) {
         // No dependent entities nothing to do.
         return FALSE;
     }
     $affected_dependencies = array('config' => array(), 'content' => array(), 'module' => array(), 'theme' => array());
     // Work out if any of the entity's dependencies are going to be affected.
     if (isset($entity_dependencies[$type])) {
         // Work out which dependencies the entity has in common with the provided
         // $type and $names.
         $affected_dependencies[$type] = array_intersect($entity_dependencies[$type], $names);
         // If the dependencies are entities we need to convert them into objects.
         if ($type == 'config' || $type == 'content') {
             $affected_dependencies[$type] = array_map(function ($name) use($type) {
                 if ($type == 'config') {
                     return $this->loadConfigEntityByName($name);
                 } else {
                     // Ignore the bundle.
                     list($entity_type_id, , $uuid) = explode(':', $name);
                     return $this->entityManager->loadEntityByConfigTarget($entity_type_id, $uuid);
                 }
             }, $affected_dependencies[$type]);
         }
     }
     // Merge any other configuration entities into the list of affected
     // dependencies if necessary.
     if (isset($entity_dependencies['config'])) {
         foreach ($dependent_entities as $dependent_entity) {
             if (in_array($dependent_entity->getConfigDependencyName(), $entity_dependencies['config'])) {
                 $affected_dependencies['config'][] = $dependent_entity;
             }
         }
     }
     // Key the entity arrays by config dependency name to make searching easy.
     foreach (['config', 'content'] as $dependency_type) {
         $affected_dependencies[$dependency_type] = array_combine(array_map(function ($entity) {
             return $entity->getConfigDependencyName();
         }, $affected_dependencies[$dependency_type]), $affected_dependencies[$dependency_type]);
     }
     // Inform the entity.
     return $entity->onDependencyRemoval($affected_dependencies);
 }
Exemplo n.º 22
0
 /**
  * Helper callback for uasort() to sort configuration entities by weight and label.
  */
 public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b)
 {
     $a_weight = isset($a->weight) ? $a->weight : 0;
     $b_weight = isset($b->weight) ? $b->weight : 0;
     if ($a_weight == $b_weight) {
         $a_label = $a->label();
         $b_label = $b->label();
         return strnatcasecmp($a_label, $b_label);
     }
     return $a_weight < $b_weight ? -1 : 1;
 }
Exemplo n.º 23
0
  /**
   * Sorts the flag entities, putting disabled flags at the bottom.
   *
   * @see \Drupal\Core\Config\Entity\ConfigEntityBase::sort()
   */
  public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) {

    // Check if the entities are flags, if not go with the default.
    if ($a instanceof FlagInterface && $b instanceof FlagInterface) {

      if ($a->isEnabled() && $b->isEnabled()) {
        return parent::sort($a, $b);
      }
      elseif (!$a->isEnabled()) {
        return -1;
      }
      elseif (!$b->isEnabled()) {
        return 1;
      }
    }

    return parent::sort($a, $b);
  }