/** * Implements Drupal\file\FileUsage\FileUsageInterface::add(). */ public function add(FileInterface $file, $module, $type, $id, $count = 1) { // Make sure that a used file is permanent. if (!$file->isPermanent()) { $file->setPermanent(); $file->save(); } }
protected function setUp() { parent::setUp(); // Use Classy theme for testing markup output. \Drupal::service('theme_handler')->install(['classy']); \Drupal::service('theme_handler')->setDefault('classy'); $this->baseUri = \Drupal::url('<front>', [], ['absolute' => TRUE]); // Create two test users. $this->adminUser = $this->drupalCreateUser(array('administer content types', 'administer comments', 'access comments', 'access content')); $this->webUser = $this->drupalCreateUser(array('access comments', 'post comments', 'skip comment approval', 'access content')); $this->drupalLogin($this->adminUser); // Create term. $this->term = Term::create(['name' => $this->randomMachineName(), 'description' => $this->randomMachineName(), 'vid' => 'tags']); $this->term->save(); // Create image. file_unmanaged_copy(\Drupal::root() . '/core/misc/druplicon.png', 'public://example.jpg'); $this->image = File::create(['uri' => 'public://example.jpg']); $this->image->save(); // Create article. $article_settings = array('type' => 'article', 'promote' => NODE_PROMOTED, 'field_image' => array(array('target_id' => $this->image->id())), 'field_tags' => array(array('target_id' => $this->term->id()))); $this->article = $this->drupalCreateNode($article_settings); // Create second article to test teaser list. $this->drupalCreateNode(array('type' => 'article', 'promote' => NODE_PROMOTED)); // Create article comment. $this->articleComment = $this->saveComment($this->article->id(), $this->webUser->id(), NULL, 0); // Create page. $this->page = $this->drupalCreateNode(array('type' => 'page')); // Set URIs. // Image. $image_file = $this->article->get('field_image')->entity; $this->imageUri = ImageStyle::load('large')->buildUrl($image_file->getFileUri()); // Term. $this->termUri = $this->term->url('canonical', array('absolute' => TRUE)); // Article. $this->articleUri = $this->article->url('canonical', array('absolute' => TRUE)); // Page. $this->pageUri = $this->page->url('canonical', array('absolute' => TRUE)); // Author. $this->authorUri = $this->adminUser->url('canonical', array('absolute' => TRUE)); // Comment. $this->articleCommentUri = $this->articleComment->url('canonical', array('absolute' => TRUE)); // Commenter. $this->commenterUri = $this->webUser->url('canonical', array('absolute' => TRUE)); $this->drupalLogout(); }
protected function setUp() { parent::setUp(); $this->base_uri = \Drupal::url('<front>', [], ['absolute' => TRUE]); // Create two test users. $this->adminUser = $this->drupalCreateUser(array('administer content types', 'administer comments', 'access comments', 'access content')); $this->webUser = $this->drupalCreateUser(array('access comments', 'post comments', 'skip comment approval', 'access content')); $this->drupalLogin($this->adminUser); // Create term. $this->term = entity_create('taxonomy_term', array('name' => $this->randomMachineName(), 'description' => $this->randomMachineName(), 'vid' => 'tags')); $this->term->save(); // Create image. file_unmanaged_copy(DRUPAL_ROOT . '/core/misc/druplicon.png', 'public://example.jpg'); $this->image = entity_create('file', array('uri' => 'public://example.jpg')); $this->image->save(); // Create article. $article_settings = array('type' => 'article', 'promote' => NODE_PROMOTED, 'field_image' => array(array('target_id' => $this->image->id())), 'field_tags' => array(array('target_id' => $this->term->id()))); $this->article = $this->drupalCreateNode($article_settings); // Create second article to test teaser list. $this->drupalCreateNode(array('type' => 'article', 'promote' => NODE_PROMOTED)); // Create article comment. $this->articleComment = $this->saveComment($this->article->id(), $this->webUser->id(), NULL, 0); // Create page. $this->page = $this->drupalCreateNode(array('type' => 'page')); // Set URIs. // Image. $image_file = $this->article->get('field_image')->entity; $this->imageUri = entity_load('image_style', 'large')->buildUrl($image_file->getFileUri()); // Term. $this->termUri = $this->term->url('canonical', array('absolute' => TRUE)); // Article. $this->articleUri = $this->article->url('canonical', array('absolute' => TRUE)); // Page. $this->pageUri = $this->page->url('canonical', array('absolute' => TRUE)); // Author. $this->authorUri = $this->adminUser->url('canonical', array('absolute' => TRUE)); // Comment. $this->articleCommentUri = $this->articleComment->url('canonical', array('absolute' => TRUE)); // Commenter. $this->commenterUri = $this->webUser->url('canonical', array('absolute' => TRUE)); $this->drupalLogout(); }
/** * @param \Drupal\file\FileInterface $file */ protected function saveFileUpload(FileInterface $file) { // Save the file to get an fid, and then create a FillPdfForm record // based off that. $file->setPermanent(); // Save the file so we can get an fid $file->save(); }