Score:0

Read comment attachment name and file location

in flag

I have a view that has comment, comment attachment(as a file). I want to read the file name and it's location. View looks like below:

enter image description here

I am able to read the title by the below code:

 public function execute(ContentEntityInterface $entity = NULL) {
    // publish the selected comment
    $entity->setPublished(TRUE)->save();

    //Read all the fields from entity
    $comment = $entity->get('comment_body')->getValue();
      //dd($comment); //throws comment title correctly
    $attachment = $entity->get('field_attachment')->getFileName();
      //dd($attachment); //errors at getFileName() function
}

I want to get the filename and file path. How to achieve it?

Kevin avatar
in flag
https://api.drupal.org/api/drupal/core%21modules%21file%21src%21Entity%21File.php/function/File%3A%3AgetFileUri/8.8.x
Jiah avatar
in flag
$attachment = $entity->get('field_attachment')->getFileUri(); results in error saying : Call to undefined method Drupal\media\Entity\Media::getFileUri(). Not sure what I'm missing
Kevin avatar
in flag
If it is a media entity, you need to get the file/image field off that entity before calling that.
Jiah avatar
in flag
$file_uri = file_create_url($entity->field_attachment->entity->getFileUri()); working for me. Now, I'm trying to make it an array to get more than 1 selected view row
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.