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

    //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?

  • 1
    – Kevin
    Commented Jul 6, 2021 at 12:06
  • $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
    – Jiah
    Commented Jul 6, 2021 at 12:56
  • If it is a media entity, you need to get the file/image field off that entity before calling that.
    – Kevin
    Commented Jul 6, 2021 at 13:13
  • 1
    $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
    – Jiah
    Commented Jul 6, 2021 at 15:01


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.