예제 #1
0
 /**
  * {@inheritdoc}
  */
 public function getCancelUrl()
 {
     if ($this->revision->getEntityType()->hasLinkTemplate('version-history')) {
         return $this->revision->toUrl('version-history');
     }
     return $this->revision->toUrl();
 }
 /**
  * {@inheritdoc}
  */
 public function save(array $form, FormStateInterface $form_state)
 {
     // Save as a new revision if requested to do so.
     if (!$form_state->isValueEmpty('revision')) {
         $this->entity->setNewRevision();
     }
     $insert = $this->entity->isNew();
     $this->entity->save();
     $context = ['@type' => $this->entity->bundle(), '%info' => $this->entity->label()];
     $logger = $this->logger($this->entity->id());
     $bundle_entity = $this->getBundleEntity();
     $t_args = ['@type' => $bundle_entity ? $bundle_entity->label() : 'None', '%info' => $this->entity->label()];
     if ($insert) {
         $logger->notice('@type: added %info.', $context);
         drupal_set_message($this->t('@type %info has been created.', $t_args));
     } else {
         $logger->notice('@type: updated %info.', $context);
         drupal_set_message($this->t('@type %info has been updated.', $t_args));
     }
     if ($this->entity->id()) {
         $form_state->setValue('id', $this->entity->id());
         $form_state->set('id', $this->entity->id());
         if ($this->entity->getEntityType()->hasLinkTemplate('collection')) {
             $form_state->setRedirectUrl($this->entity->toUrl('collection'));
         } else {
             $form_state->setRedirectUrl($this->entity->toUrl('canonical'));
         }
     } else {
         // In the unlikely case something went wrong on save, the entity will be
         // rebuilt and entity form redisplayed.
         drupal_set_message($this->t('The entity could not be saved.'), 'error');
         $form_state->setRebuild();
     }
 }
예제 #3
0
 /**
  * {@inheritdoc}
  */
 public function buildRow(EntityInterface $entity)
 {
     /** @var \Drupal\conference_sessions\Entity\RoomTypeInterface $entity */
     $product_type = RoomType::load($entity->bundle());
     $row['title']['data'] = ['#type' => 'link', '#title' => $entity->label()] + $entity->toUrl()->toRenderArray();
     $row['type'] = $product_type->label();
     $row['status'] = $entity->isPublished() ? $this->t('Published') : $this->t('Unpublished');
     return $row + parent::buildRow($entity);
 }
예제 #4
0
 /**
  * {@inheritdoc}
  */
 public function buildRow(EntityInterface $entity)
 {
     $langcode = $entity->language()->getId();
     $uri = $entity->toUrl();
     $options = $uri->getOptions();
     $options += $langcode != LanguageInterface::LANGCODE_NOT_SPECIFIED && isset($languages[$langcode]) ? array('language' => $languages[$langcode]) : array();
     $uri->setOptions($options);
     $row['name']['data'] = array('#type' => 'link', '#title' => $entity->label(), '#url' => $uri);
     $row['scales']['data'] = array('#type' => 'markup', '#markup' => implode(', ', $entity->getScales()));
     return $row + parent::buildRow($entity);
 }
예제 #5
0
 /**
  * {@inheritdoc}
  */
 public function buildRow(EntityInterface $entity)
 {
     $langcode = $entity->language()->getId();
     $uri = $entity->toUrl();
     $options = $uri->getOptions();
     $options += $langcode != LanguageInterface::LANGCODE_NOT_SPECIFIED && isset($languages[$langcode]) ? array('language' => $languages[$langcode]) : array();
     $uri->setOptions($options);
     $row['name']['data'] = array('#type' => 'link', '#title' => $entity->label(), '#url' => $uri);
     $row['source']['data'] = $entity->getSource() ? t('online') : t('offline');
     $row['grade_valuation_type']['data'] = $entity->getGradeValuationType();
     $row['grade_display_type']['data'] = $entity->getGradeDisplayType();
     $row['multiplicator']['data'] = $entity->getMultiplicator();
     return $row + parent::buildRow($entity);
 }
예제 #6
0
 /**
  * {@inheritdoc}
  */
 public function buildRow(EntityInterface $entity)
 {
     $langcode = $entity->language()->getId();
     $uri = $entity->toUrl();
     $options = $uri->getOptions();
     $options += $langcode != LanguageInterface::LANGCODE_NOT_SPECIFIED && isset($languages[$langcode]) ? array('language' => $languages[$langcode]) : array();
     $uri->setOptions($options);
     $row['name']['data'] = array('#type' => 'link', '#title' => $entity->label(), '#url' => $uri);
     $row['lowest']['data'] = $entity->getLowest();
     $row['highest']['data'] = $entity->getHighest();
     $row['pass']['data'] = $entity->getPass();
     $row['hidden']['data'] = $entity->getHidden();
     $row['locked']['data'] = $entity->getLocked();
     return $row + parent::buildRow($entity);
 }
예제 #7
0
 /**
  * {@inheritdoc}
  */
 public function buildRow(EntityInterface $entity)
 {
     $langcode = $entity->language()->getId();
     $uri = $entity->toUrl();
     $options = $uri->getOptions();
     $options += $langcode != LanguageInterface::LANGCODE_NOT_SPECIFIED && isset($languages[$langcode]) ? array('language' => $languages[$langcode]) : array();
     $uri->setOptions($options);
     $row['name']['data'] = array('#type' => 'link', '#title' => $entity->label(), '#url' => $uri);
     $row['display_name'] = $entity->getDisplayName();
     $row['grade_aggregation_type'] = $entity->getGradeAggregationType();
     $row['exclude_empty'] = $entity->getExcludeEmpty() ? t('Yes') : t('No');
     $row['drop_lowest'] = $entity->getDropLowest();
     $row['author']['data'] = array('#theme' => 'username', '#account' => $entity->getOwner());
     return $row + parent::buildRow($entity);
 }
예제 #8
0
 /**
  * {@inheritdoc}
  */
 public function buildRow(EntityInterface $entity)
 {
     /** @var \Drupal\profile\Entity\ProfileInterface $entity */
     $langcode = $entity->language()->getId();
     $uri = $entity->toUrl();
     $options = $uri->getOptions();
     $options += $langcode != LanguageInterface::LANGCODE_NOT_SPECIFIED && isset($languages[$langcode]) ? ['language' => $languages[$langcode]] : [];
     $uri->setOptions($options);
     $row['label'] = $entity->link();
     $row['type'] = $entity->getType();
     $row['owner']['data'] = ['#theme' => 'username', '#account' => $entity->getOwner()];
     $row['status'] = $entity->isActive() ? $this->t('active') : $this->t('not active');
     $row['is_default'] = $entity->isDefault() ? $this->t('default') : $this->t('not default');
     $row['changed'] = $this->dateFormatter->format($entity->getChangedTime(), 'short');
     $language_manager = \Drupal::languageManager();
     if ($language_manager->isMultilingual()) {
         $row['language_name'] = $language_manager->getLanguageName($langcode);
     }
     return $row + parent::buildRow($entity);
 }
예제 #9
0
 /**
  * {@inheritdoc}
  */
 public function buildRow(EntityInterface $entity)
 {
     /** @var \Drupal\profile\Entity\ProfileInterface $entity */
     $mark = ['#theme' => 'mark', '#mark_type' => node_mark($entity->id(), $entity->getChangedTime())];
     $langcode = $entity->language()->id;
     $uri = $entity->toUrl();
     $options = $uri->getOptions();
     $options += $langcode != LanguageInterface::LANGCODE_NOT_SPECIFIED && isset($languages[$langcode]) ? ['language' => $languages[$langcode]] : [];
     $uri->setOptions($options);
     $row['label']['data'] = ['#type' => 'link', '#title' => $entity->label(), '#suffix' => ' ' . $this->renderer->render($mark)] + $uri->toRenderArray();
     $row['type'] = $entity->getType()->id();
     $row['owner']['data'] = ['#theme' => 'username', '#account' => $entity->getOwner()];
     $row['status'] = $entity->isActive() ? $this->t('active') : $this->t('not active');
     $row['changed'] = $this->dateFormatter->format($entity->getChangedTime(), 'short');
     $language_manager = \Drupal::languageManager();
     if ($language_manager->isMultilingual()) {
         $row['language_name'] = $language_manager->getLanguageName($langcode);
     }
     $route_params = ['user' => $entity->getOwnerId(), 'type' => $entity->bundle(), 'profile' => $entity->id()];
     $links['edit'] = ['title' => t('Edit'), 'route_name' => 'entity.profile.edit_form', 'route_parameters' => $route_params];
     $links['delete'] = ['title' => t('Delete'), 'route_name' => 'entity.profile.delete_form', 'route_parameters' => $route_params];
     $row[] = ['data' => ['#type' => 'operations', '#links' => $links]];
     return $row + parent::buildRow($entity);
 }
예제 #10
0
 /**
  * Creates a \Drupal\Core\Url object based on the entity and link template.
  *
  * @param \Drupal\Core\Entity\EntityInterface $entity
  *   The test entity.
  * @param string $link_template
  *   The link template.
  * @param string $langcode
  *   The langcode.
  *
  * @return \Drupal\Core\Url
  *   The URL for this entity's link template.
  */
 protected function getTestToUrl(EntityInterface $entity, $link_template, array $options = [], $langcode = NULL)
 {
     $entity_type = $this->getMock('Drupal\\Core\\Entity\\EntityTypeInterface');
     $entity_type->expects($this->any())->method('getLinkTemplates')->will($this->returnValue(array('edit-form' => 'test_entity_type.edit')));
     if ($langcode) {
         $entity->langcode = $langcode;
     }
     $this->entityManager->expects($this->any())->method('getDefinition')->with('test_entity_type')->will($this->returnValue($entity_type));
     // If no link template is given, call without a value to test the default.
     if ($link_template) {
         $uri = $entity->toUrl($link_template, $options);
     } else {
         if ($entity instanceof ConfigEntityInterface) {
             $uri = $entity->toUrl('edit-form', $options);
         } else {
             $uri = $entity->toUrl('canonical', $options);
         }
     }
     return $uri;
 }
 /**
  * {@inheritdoc}
  */
 protected function buildDeleteRevisionLink(EntityInterface $entity_revision)
 {
     if ($entity_revision->hasLinkTemplate('revision-delete')) {
         return ['title' => t('Delete'), 'url' => $entity_revision->toUrl('revision-delete')];
     }
 }
예제 #12
0
  /**
   * Patches an existing entity using the passed in (modified) entity.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The updated entity to send.
   * @param string[] $read_only_fields
   *   Names of the fields that are read-only, in validation order.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The account to use for serialization.
   * @param string $format
   *   A serialization format.
   * @param string $mime_type
   *   The MIME type corresponding to the specified serialization format.
   */
  protected function patchEntity(EntityInterface $entity, array $read_only_fields, AccountInterface $account, $format, $mime_type) {
    $serializer = $this->container->get('serializer');

    $url = $entity->toUrl()->setRouteParameter('_format', $this->defaultFormat);
    $context = ['account' => $account];
    // Certain fields are always read-only, others this user simply is not
    // allowed to modify. For all of them, ensure they are not serialized, else
    // we'll get a 403 plus an error message.
    for ($i = 0; $i < count($read_only_fields); $i++) {
      $field = $read_only_fields[$i];

      $normalized = $serializer->normalize($entity, $format, $context);
      if ($format !== 'hal_json') {
        // The default normalizer always keeps fields, even if they are unset
        // here because they should be omitted during a PATCH request. Therefore
        // manually strip them
        // @see \Drupal\Core\Entity\ContentEntityBase::__unset()
        // @see \Drupal\serialization\Normalizer\EntityNormalizer::normalize()
        // @see \Drupal\hal\Normalizer\ContentEntityNormalizer::normalize()
        $read_only_fields_so_far = array_slice($read_only_fields, 0, $i);
        $normalized = array_diff_key($normalized, array_flip($read_only_fields_so_far));
      }
      $serialized = $serializer->serialize($normalized, $format, $context);

      $this->httpRequest($url, 'PATCH', $serialized, $mime_type);
      $this->assertResponse(403);
      $this->assertResponseBody('{"message":"Access denied on updating field \\u0027' . $field . '\\u0027."}');

      if ($format === 'hal_json') {
        // We've just tried with this read-only field, now unset it.
        $entity->set($field, NULL);
      }
    }

    // Finally, with all read-only fields unset, the request should succeed.
    $normalized = $serializer->normalize($entity, $format, $context);
    if ($format !== 'hal_json') {
      $normalized = array_diff_key($normalized, array_combine($read_only_fields, $read_only_fields));
    }
    $serialized = $serializer->serialize($normalized, $format, $context);

    // Try first without CSRF token which should fail.
    $this->httpRequest($url, 'PATCH', $serialized, $mime_type, FALSE);
    $this->assertResponse(403);
    $this->assertRaw('X-CSRF-Token request header is missing');
    // Then try with an invalid CSRF token.
    $this->httpRequest($url, 'PATCH', $serialized, $mime_type, 'invalid-csrf-token');
    $this->assertResponse(403);
    $this->assertRaw('X-CSRF-Token request header is invalid');
    // Then try with CSRF token.
    $this->httpRequest($url, 'PATCH', $serialized, $mime_type);
    $this->assertResponse(200);
  }
예제 #13
0
 /**
  * Gets the read URL object for the entity.
  *
  * @param \Drupal\Core\Entity\EntityInterface $entity
  *   The entity to get the URL for.
  * @param string $format
  *   The format to request the entity in.
  * @param string $entity_id
  *   The entity ID to use in the URL, defaults to the entity's ID if know
  *   given.
  *
  * @return \Drupal\Core\Url
  *   The Url object.
  */
 protected function getReadUrl(EntityInterface $entity, $format = NULL, $entity_id = NULL)
 {
     if (!$format) {
         $format = $this->defaultFormat;
     }
     if (!$entity_id) {
         $entity_id = $entity->id();
     }
     $entity_type = $entity->getEntityTypeId();
     if ($entity->hasLinkTemplate('canonical')) {
         $url = $entity->toUrl('canonical');
     } else {
         $route_name = 'rest.entity.' . $entity_type . ".GET.";
         // If testing unsupported format don't use the format to construct route
         // name. This would give a RouteNotFoundException.
         if ($format == 'wrongformat') {
             $route_name .= $this->defaultFormat;
         } else {
             $route_name .= $format;
         }
         $url = Url::fromRoute($route_name);
     }
     $url->setRouteParameter($entity_type, $entity_id);
     $url->setRouteParameter('_format', $format);
     return $url;
 }
 /**
  * {@inheritdoc}
  */
 public function getCancelUrl() {
   return $this->entity->toUrl();
 }
예제 #15
0
 /**
  * Builds the path string used in the match array.
  *
  * @param \Drupal\Core\Entity\EntityInterface $entity
  *    The matched entity.
  *
  * @return string
  *   The URL for this entity.
  */
 protected function buildPath($entity) {
   return $entity->toUrl()->toString();
 }
예제 #16
0
 function saveEntityAlias($entity_type, EntityInterface $entity, $alias, $language = Language::LANGCODE_NOT_SPECIFIED)
 {
     $uri = $entity->toUrl()->toArray();
     return $this->saveAlias($uri['path'], $alias, $language);
 }
 public function assertNoEntityAliasExists(EntityInterface $entity, $alias = NULL)
 {
     $path = array('source' => '/' . $entity->toUrl()->getInternalPath());
     if (!empty($alias)) {
         $path['alias'] = $alias;
     }
     $this->assertNoAliasExists($path);
 }
예제 #18
0
 /**
  * {@inheritdoc}
  */
 public function deleteEntityPathAll(EntityInterface $entity, $default_uri = NULL)
 {
     $this->deleteAll('/' . $entity->toUrl('canonical')->getInternalPath());
     if (isset($default_uri) && $entity->toUrl('canonical')->toString() != $default_uri) {
         $this->deleteAll($default_uri);
     }
 }
예제 #19
0
 /**
  * Creates an item for the given entity.
  *
  * @param \Drupal\Core\Entity\EntityInterface $entity
  *   The entity.
  *
  * @return array|null
  *   NULL if the entity should not be displayed. Otherwise, an associative
  *   array with at least "value" set, to either a string or a render array,
  *   and possibly also additional alter options.
  */
 protected function getItem(EntityInterface $entity)
 {
     $bundle = $entity->bundle();
     if (empty($this->options['display_methods'][$bundle]['display_method'])) {
         return NULL;
     }
     $display_method = $this->options['display_methods'][$bundle]['display_method'];
     if (in_array($display_method, array('id', 'label'))) {
         if ($display_method == 'label') {
             $item['value'] = $entity->label();
         } else {
             $item['value'] = $entity->id();
         }
         if ($this->options['link_to_item']) {
             $item['make_link'] = TRUE;
             $item['url'] = $entity->toUrl('canonical');
         }
         return $item;
     }
     $view_mode = $this->options['display_methods'][$bundle]['view_mode'];
     $build = $this->getEntityManager()->getViewBuilder($entity->getEntityTypeId())->view($entity, $view_mode);
     return array('value' => $build);
 }