Repository: jekyll/jekyll (Ruby)

Issue:
[Bug]: `name` attributes defined in front matter are rendered incorrectly with Jekyll 4.3.0
### Operating System

Debian 11

### Ruby Version

ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux]

### Jekyll Version

Jekyll 4.3.0

### GitHub Pages Version

Latest

### Expected Behavior

If a collection declare the `name` attribute its document front matter, the attribute value should be rendered in the templates that are referencing this attribute.

### Current Behavior

When referencing the `name` attribute, the markdown filename is rendered instead of the attribute value. Other attributes are rendered as expected.

### Relevant log output

_No response_

### Code Sample

On `main` branch (Jekyll 4.2.2) everything works fine :
- clone https://github.com/lescastcodeurs/lescastcodeurs.com
- install dependencies (`bundle install`)
- serve the site `bundle exec jekyll serve --livereload --trace`
- go to http://127.0.0.1:4000/equipe/
- team members names are correctly rendered with the `name` specified in the metadata (such as `Antonio Goncalves`)

On `jekyll-4.3.0` branch (Jekyll 4.3.0) `name` attributes are not rendered as expected :
- clone https://github.com/lescastcodeurs/lescastcodeurs.com
- switch the the `jekyll-4.3.0` branch
- install dependencies (`bundle install`)
- serve the site `bundle exec jekyll serve --livereload --trace`
- go to http://127.0.0.1:4000/equipe/
- team members names are incorrectly rendered with the markdown filenames (such as `AntonioGoncalves.md`)


Discussion:
I do not know if it's related, but there was this change in Jekyll 4.3.0 : _Expose basename from document.rb as name to Liquid templates (#8761)_.
Other attributes are rendered as expected. A workaround is to use a different name for the attribute.
Hello @marcwrobel 
You're right in stating that this issue is related to the mentioned change in v4.3.0.
It is unfortunate that your project is encountering this issue. I admit that I had overlooked the possibility of a conflict with front matter definition when accepting the change.
I will release a v4.3.1 some time in next week.
*Addendum: @marcwrobel The description in this ticket is incorrect. Your project doesn't have `name` attribute defined in the collection metadata but instead it is defined in the documents' front matter.*
Just opened an issue on this as well, #9165. Can confirm we are experiencing this and noticed issues with our site this morning. Reverting to Jekyll `v4.2.2` resolved it. 

I would like to leave #9165 open, as we are experiencing another issue where we are seeing a Jekyll hash rather than the object of data when using our `_data` collection.
Thanks for the quick fix @ashmaroli.



Patch:
diff --git a/lib/jekyll/drops/document_drop.rb b/lib/jekyll/drops/document_drop.rb
index 00e51e20594..0cff3741074 100644
--- a/lib/jekyll/drops/document_drop.rb
+++ b/lib/jekyll/drops/document_drop.rb
@@ -12,7 +12,6 @@ class DocumentDrop < Drop
       mutable false
 
       delegate_method_as :relative_path, :path
-      delegate_method_as :basename, :name
       private delegate_method_as :data, :fallback_data
 
       delegate_methods :id, :output, :content, :to_s, :relative_path, :url, :date
@@ -26,6 +25,10 @@ def excerpt
         fallback_data["excerpt"].to_s
       end
 
+      def name
+        fallback_data["name"] || @obj.basename
+      end
+
       def <=>(other)
         return nil unless other.is_a? DocumentDrop
 
diff --git a/lib/jekyll/drops/excerpt_drop.rb b/lib/jekyll/drops/excerpt_drop.rb
index 64a8aa7daaa..82d3cdf827b 100644
--- a/lib/jekyll/drops/excerpt_drop.rb
+++ b/lib/jekyll/drops/excerpt_drop.rb
@@ -16,7 +16,7 @@ def excerpt
       end
 
       def name
-        @obj.doc.basename
+        @obj.doc.data["name"] || @obj.doc.basename
       end
     end
   end


Test patch:
diff --git a/test/source/_roles/named.md b/test/source/_roles/named.md
new file mode 100644
index 00000000000..dfb6847614a
--- /dev/null
+++ b/test/source/_roles/named.md
@@ -0,0 +1,5 @@
+---
+name: launcher
+---
+
+`name` defined in front matter.
diff --git a/test/source/_roles/unnamed.md b/test/source/_roles/unnamed.md
new file mode 100644
index 00000000000..c35bf6b3a0a
--- /dev/null
+++ b/test/source/_roles/unnamed.md
@@ -0,0 +1,4 @@
+---
+---
+
+No `name` in front matter.
diff --git a/test/test_document.rb b/test/test_document.rb
index e980b1f5dc3..f5f5cce6ec0 100644
--- a/test/test_document.rb
+++ b/test/test_document.rb
@@ -159,6 +159,23 @@ def setup_document_with_dates(filename)
     should "output its relative path as path in Liquid" do
       assert_equal "_methods/configuration.md", @document.to_liquid["path"]
     end
+
+    context "when rendered with Liquid" do
+      should "respect the front matter definition" do
+        site = fixture_site("collections" => ["roles"]).tap(&:process)
+        docs = site.collections["roles"].docs
+
+        # Ruby context: doc.basename is aliased as doc.to_liquid["name"] by default.
+
+        document = docs.detect { |d| d.relative_path == "_roles/unnamed.md" }
+        assert_equal "unnamed.md", document.basename
+        assert_equal "unnamed.md", document.to_liquid["name"]
+
+        document = docs.detect { |d| d.relative_path == "_roles/named.md" }
+        assert_equal "named.md", document.basename
+        assert_equal "launcher", document.to_liquid["name"]
+      end
+    end
   end
 
   context "a document as part of a collection with front matter defaults" do