1

I'm going through(learning) class reference TableFieldType in Craft 2 and I'm having trouble pulling out the value from a table field and save it as variable. I'm not sure I've started with the right approach.

This is the piece of code I'm working on where contact details are saved to the table field. The problem starts when I need to get the email address value from that table field and use it further on.

public function init()
{
    craft()->on('guestEntries.success', function (GuestEntriesEvent $event)
    {

        /** @var EntryModel $entryModel */
        $entryModel = $event->params['entry'];

        if($entryModel->getSection()->handle === "orderId")
        {

          $emailSettings = craft()->email->getSettings();
          $email         = new EmailModel;
          $emailAddress = '';

          // Questionable, not complete block of code 
          // Get value of "emailAddress" singleline type column from the "contactDetails" table field type
          foreach ($entryModel->getFieldLayout()->getFields() as $fieldLayout)
          {
            $field = $fieldLayout->getField();

            if ($field->getFieldType()->getName() == Craft::t('Table'))
            {

              foreach ($entryModel->{$field->myHandle}-> ??? as $input)
              {
                 $emailAddress = $input->???;
              } 
            }
          }

          $email->toFirstName = $entryModel->getAuthor()->firstName;
          $email->toLastName  = $entryModel->getAuthor()->lastName;

          // Test if email address from "contactDetails" table field is present
          $email->toEmail     = !empty($emailAddress) ? $emailAddress : $entryModel->getAuthor()->email;

          $email->fromEmail   = $emailSettings['emailAddress'];
          $email->replyTo     = $emailSettings['emailAddress'];
          $email->sender      = $emailSettings['emailAddress'];
          $email->fromName    = craft()->config->get('siteName');
          $email->subject     = craft()->config->get('siteName') . " - New order received";
          $email->body        = "...";
          craft()->email->sendEmail($email);

        }

    });
}
0
1

You can grab the content of a field for an element with

$myTableField = $entry->getContent()->getAttribute('fieldHandle');

You can modify the variable and send it back with

$entry->getContent()->setAttribute('fieldHandle', $myTableField);

Since you are probably in an event you should make sure your entry has the correct type and the correct section

$entry->getSection()->handle === 'mySectionHandle' && $entry->getType()->handle === 'typeHandle'

If you want to change every table field and not just a certain one you can do

foreach($entry->getFieldLayout()->getFields() as $field){
    if($field::class === 'insert the class of the field here'){
        $table = $entry->getContent()->getAttribute($field->handle);
   } 
} 
3
  • I'm not sure this is the way to solve it. Perhaps I put in little information about the problem. I updated the question. Anyway I've tested it with your approach and the email address field was empty when sending email. May 18 '18 at 11:36
  • You can grab all available attributes for the element with $entry->getContent()->getAttributes() maybe you tried to access the wrong field handle? However you should have mentioned you are using the guest entry plugin. Maybe this one handles everything differently. I don't know it. My explanation is only for Craft May 18 '18 at 12:02
  • I'm carefully with handles. I'll keep digging. Thank you for your time @Robin Schambach May 18 '18 at 12:13

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.