1

I create a new filed type and attached it to entries. Now I need to get the entry id which the field is attached to.

I tried $this->element in my field class, but got an error said that I'm calling an unknown property.

Any way to do that please?

1

It may depend on where in the request you need to access the element.

If you need to access the element in a Field class on your Entry Edit page, the getInputHtml method may be useful:

public function getInputHtml($value, ElementInterface $element = null): string
{
    // $element stuff

    return Craft::$app->getView()->renderTemplate('my-plugin/field',
    [
        'field' => $this,

        // make the Element available in your field's input template
        'element' => $element
    ]);
}

If you need to access the element in a Field class after the Element is saved there is the afterSaveElement method:

public function afterElementSave(ElementInterface $element, bool $isNew)
{
    parent::afterElementSave($element, $isNew);

    // $element stuff
}

Note: The Entry ID may not exist while a new entry is being created. Accessing the Element via the afterElementSave ensures that the Entry has been saved and has an ID.

|improve this answer|||||
  • Thanks! But I hope I can get the entry id after I open the entry editing page in CP. So this function is not for me. – yu xia Jun 5 '18 at 3:15
  • @yuxia then the getInputHtml docs.craftcms.com/api/v3/… might be something for you. – Robin Schambach Jun 5 '18 at 4:50
  • I've updated my answer to include the getInputHtml method Robin mentions as well. – Ben Parizek Jun 6 '18 at 13:35
  • @BenParizek Thanks for your code. But I don't understand what the getInputHtml do. Maybe I didn't make myself clear. Basically there are two Asset field types: A and B. Let's say a user selected three images to A, Ctrl+s to save and then he can only select these three images to B. The field B is what I want to create. afterElementSave has the limit which is users have to save to trigger this function. – yu xia Jun 6 '18 at 23:23
  • @BenParizek You are absolutely right! I can get element via getInputHtml. But I have to rewrite the template in this function which is gonna take too much effort – yu xia Jun 7 '18 at 3:11
0

Way one: Inside the field class, there is a inputTemplateVariables function with two params, like this

protected function inputTemplateVariables($value = null, ElementInterface $element = null): array
{
     $vars = parent::inputTemplateVariables($value, $element);

     $settings = $this->getSettings();

     //here you can get element
     $entryId = $element->id;

     return $vars;
}

Way two: In your plugin's main file, put codes below into init() function

Craft::$app->getView()->hook('cp.entries.edit.details', function (&$context) 
{

     //here you can also get the element id
     //you can save it into session and use it anywhere you need
     $id = $context['entry']['id'];
});
|improve this answer|||||

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.