1

I'm trying to create a template route that allows me to get all of the attorneys that I've related to a specified practice area. I can get the slug easily, as my route looks like this:

'attorneys/legal-services/(?P<slug>[^/]+)' => 'attorneys/legal-services'

And sure enough, when I go to /attorneys/legal-services/construction-law, I can access the {{ slug }} variable and grab construction-law.

The problem I'm having is that everything else in the object I request with craft.entries.slug(slug) ends up being NULL.

A simple code sample like this:

{% extends '_layouts/two-column' %}
{% set practice = craft.entries.slug(slug) %}

{% block main %}
  <main class="page-body">
    <h1>Attorneys</h1>
    <h2>in {{ practice.title }}</h2>
    {% for entry in craft.entries.section('people').relatedTo(practice) %}
      <a href="{{ entry.url }}">{{ entry.title }}</a>
    {% endfor %}
  </main>
{% endblock %}

will output all the content I request in my for loop, but practice.title is left empty.

When I dump the contents of the practice object out, I see this:

object(Craft\ElementCriteriaModel)[749]
  protected 'strictAttributes' => boolean false
  private '_elementType' => 
    object(Craft\EntryElementType)[742]
      protected 'componentType' => string 'ElementType' (length=11)
      private '_sourcesByContext' (Craft\BaseElementType) => null
      private '_classHandle' (Craft\BaseComponentType) => null
      public 'behaviors' => 
        array (size=0)
          empty
      private '_initialized' (CApplicationComponent) => boolean true
      private '_e' (CComponent) => null
      private '_m' (CComponent) => null
  private '_supportedFieldHandles' => null
  private '_matchedElements' => null
  private '_matchedElementsAtOffsets' => null
  private '_cachedIds' => null
  private '_cachedTotal' => null
  protected 'classSuffix' => string 'Model' (length=5)
  private '_classHandle' (Craft\BaseModel) => null
  private '_attributeConfigs' (Craft\BaseModel) => 
    array (size=45)
      'ancestorDist' => 
        array (size=8)
          'maxLength' => int 11
          'min' => int -2147483648
          'max' => int 2147483647
          'decimals' => int 0
          'type' => string 'number' (length=6)
          'unsigned' => boolean false
          'length' => int 10
          'column' => string 'integer' (length=7)
      'ancestorOf' => 
        array (size=3)
          'model' => null
          'column' => string 'text' (length=4)
          'type' => string 'mixed' (length=5)
      'archived' => 
        array (size=8)
          'maxLength' => int 1
          'default' => boolean false
          'required' => boolean true
          'column' => string 'tinyint' (length=7)
          'unsigned' => boolean true
          'type' => string 'bool' (length=4)
          'min' => int 0
          'max' => int 255
      'dateCreated' => 
        array (size=3)
          'model' => null
          'column' => string 'text' (length=4)
          'type' => string 'mixed' (length=5)
      'dateUpdated' => 
        array (size=3)
          'model' => null
          'column' => string 'text' (length=4)
          'type' => string 'mixed' (length=5)
      'descendantDist' => 
        array (size=8)
          'maxLength' => int 11
          'min' => int -2147483648
          'max' => int 2147483647
          'decimals' => int 0
          'type' => string 'number' (length=6)
          'unsigned' => boolean false
          'length' => int 10
          'column' => string 'integer' (length=7)
      'descendantOf' => 
        array (size=3)
          'model' => null
          'column' => string 'text' (length=4)
          'type' => string 'mixed' (length=5)
      'fixedOrder' => 
        array (size=8)
          'maxLength' => int 1
          'default' => boolean false
          'required' => boolean true
          'column' => string 'tinyint' (length=7)
          'unsigned' => boolean true
          'type' => string 'bool' (length=4)
          'min' => int 0
          'max' => int 255
      'id' => 
        array (size=8)
          'maxLength' => int 11
          'min' => int -2147483648
          'max' => int 2147483647
          'decimals' => int 0
          'type' => string 'number' (length=6)
          'unsigned' => boolean false
          'length' => int 10
          'column' => string 'integer' (length=7)
      'indexBy' => 
        array (size=1)
          'type' => string 'string' (length=6)
      'level' => 
        array (size=8)
          'maxLength' => int 11
          'min' => int -2147483648
          'max' => int 2147483647
          'decimals' => int 0
          'type' => string 'number' (length=6)
          'unsigned' => boolean false
          'length' => int 10
          'column' => string 'integer' (length=7)
      'limit' => 
        array (size=9)
          'maxLength' => int 11
          'min' => int -2147483648
          'max' => int 2147483647
          'decimals' => int 0
          'default' => int 100
          'type' => string 'number' (length=6)
          'unsigned' => boolean false
          'length' => int 10
          'column' => string 'integer' (length=7)
      'locale' => 
        array (size=2)
          'column' => string 'locale' (length=6)
          'type' => string 'locale' (length=6)
      'localeEnabled' => 
        array (size=8)
          'maxLength' => int 1
          'default' => boolean true
          'required' => boolean true
          'column' => string 'tinyint' (length=7)
          'unsigned' => boolean true
          'type' => string 'bool' (length=4)
          'min' => int 0
          'max' => int 255
      'nextSiblingOf' => 
        array (size=3)
          'model' => null
          'column' => string 'text' (length=4)
          'type' => string 'mixed' (length=5)
      'offset' => 
        array (size=9)
          'maxLength' => int 11
          'min' => int -2147483648
          'max' => int 2147483647
          'decimals' => int 0
          'default' => int 0
          'type' => string 'number' (length=6)
          'unsigned' => boolean false
          'length' => int 10
          'column' => string 'integer' (length=7)
      'order' => 
        array (size=2)
          'default' => string 'lft, postDate desc' (length=18)
          'type' => string 'string' (length=6)
      'positionedAfter' => 
        array (size=3)
          'model' => null
          'column' => string 'text' (length=4)
          'type' => string 'mixed' (length=5)
      'positionedBefore' => 
        array (size=3)
          'model' => null
          'column' => string 'text' (length=4)
          'type' => string 'mixed' (length=5)
      'prevSiblingOf' => 
        array (size=3)
          'model' => null
          'column' => string 'text' (length=4)
          'type' => string 'mixed' (length=5)
      'relatedTo' => 
        array (size=3)
          'model' => null
          'column' => string 'text' (length=4)
          'type' => string 'mixed' (length=5)
      'ref' => 
        array (size=1)
          'type' => string 'string' (length=6)
      'search' => 
        array (size=1)
          'type' => string 'string' (length=6)
      'siblingOf' => 
        array (size=3)
          'model' => null
          'column' => string 'text' (length=4)
          'type' => string 'mixed' (length=5)
      'slug' => 
        array (size=1)
          'type' => string 'string' (length=6)
      'status' => 
        array (size=2)
          'default' => string 'live' (length=4)
          'type' => string 'string' (length=6)
      'title' => 
        array (size=1)
          'type' => string 'string' (length=6)
      'uri' => 
        array (size=1)
          'type' => string 'string' (length=6)
      'with' => 
        array (size=3)
          'model' => null
          'column' => string 'text' (length=4)
          'type' => string 'mixed' (length=5)
      'childField' => 
        array (size=1)
          'type' => string 'string' (length=6)
      'childOf' => 
        array (size=3)
          'model' => null
          'column' => string 'text' (length=4)
          'type' => string 'mixed' (length=5)
      'depth' => 
        array (size=8)
          'maxLength' => int 11
          'min' => int -2147483648
          'max' => int 2147483647
          'decimals' => int 0
          'type' => string 'number' (length=6)
          'unsigned' => boolean false
          'length' => int 10
          'column' => string 'integer' (length=7)
      'parentField' => 
        array (size=1)
          'type' => string 'string' (length=6)
      'parentOf' => 
        array (size=3)
          'model' => null
          'column' => string 'text' (length=4)
          'type' => string 'mixed' (length=5)
      'after' => 
        array (size=3)
          'model' => null
          'column' => string 'text' (length=4)
          'type' => string 'mixed' (length=5)
      'authorGroup' => 
        array (size=1)
          'type' => string 'string' (length=6)
      'authorGroupId' => 
        array (size=8)
          'maxLength' => int 11
          'min' => int -2147483648
          'max' => int 2147483647
          'decimals' => int 0
          'type' => string 'number' (length=6)
          'unsigned' => boolean false
          'length' => int 10
          'column' => string 'integer' (length=7)
      'authorId' => 
        array (size=8)
          'maxLength' => int 11
          'min' => int -2147483648
          'max' => int 2147483647
          'decimals' => int 0
          'type' => string 'number' (length=6)
          'unsigned' => boolean false
          'length' => int 10
          'column' => string 'integer' (length=7)
      'before' => 
        array (size=3)
          'model' => null
          'column' => string 'text' (length=4)
          'type' => string 'mixed' (length=5)
      'editable' => 
        array (size=8)
          'maxLength' => int 1
          'default' => boolean false
          'required' => boolean true
          'column' => string 'tinyint' (length=7)
          'unsigned' => boolean true
          'type' => string 'bool' (length=4)
          'min' => int 0
          'max' => int 255
      'expiryDate' => 
        array (size=3)
          'model' => null
          'column' => string 'text' (length=4)
          'type' => string 'mixed' (length=5)
      'postDate' => 
        array (size=3)
          'model' => null
          'column' => string 'text' (length=4)
          'type' => string 'mixed' (length=5)
      'section' => 
        array (size=3)
          'model' => null
          'column' => string 'text' (length=4)
          'type' => string 'mixed' (length=5)
      'sectionId' => 
        array (size=8)
          'maxLength' => int 11
          'min' => int -2147483648
          'max' => int 2147483647
          'decimals' => int 0
          'type' => string 'number' (length=6)
          'unsigned' => boolean false
          'length' => int 10
          'column' => string 'integer' (length=7)
      'type' => 
        array (size=3)
          'model' => null
          'column' => string 'text' (length=4)
          'type' => string 'mixed' (length=5)
  private '_attributes' (Craft\BaseModel) => 
    array (size=9)
      'archived' => boolean false
      'fixedOrder' => boolean false
      'limit' => int 100
      'localeEnabled' => boolean true
      'offset' => int 0
      'order' => string 'lft, postDate desc' (length=18)
      'status' => string 'live' (length=4)
      'editable' => boolean false
      'slug' => string 'construction-law' (length=16)
  private '_extraAttributeNames' (Craft\BaseModel) => 
    array (size=0)
      empty
  private '_errors' (CModel) => 
    array (size=0)
      empty
  private '_validators' (CModel) => null
  private '_scenario' (CModel) => string '' (length=0)
  private '_e' (CComponent) => null
  private '_m' (CComponent) => null

As a result, I'm getting

<h1>Attorneys</h1>
<h2>in </h2>

even though I'm expecting to see the title of the practice area.

4

You will need to use {% set practice = craft.entries.slug(slug).first() %} to fetch the real EntryModel, and not just an ElementCriteriaModel.

I think the relatedTo criteria still works without it because that calls .find() implicitly when passed an ElementCriteriaModel, but that doesn't actually change what the practice variable references in the rest of the template.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.