Bolt\Twig\Handler\RecordHandler::selectField PHP Метод

selectField() публичный Метод

Return a selected field from a contentset.
public selectField ( array $content, array | string $fieldName, boolean $startempty = false, string $keyName = 'id' ) : array
$content array A Bolt record array
$fieldName array | string Name of a field, or array of field names to return from each record
$startempty boolean Whether or not the array should start with an empty element
$keyName string Name of the key in the array
Результат array
    public function selectField($content, $fieldName, $startempty = false, $keyName = 'id')
    {
        $retval = $startempty ? [] : ['' => ''];
        if (empty($content)) {
            return $retval;
        }
        foreach ($content as $c) {
            $element = $c->values[$keyName];
            if (is_array($fieldName)) {
                $row = [];
                foreach ($fieldName as $fn) {
                    $row[] = isset($c->values[$fn]) ? $c->values[$fn] : null;
                }
                $retval[$element] = $row;
            } elseif (isset($c->values[$fieldName])) {
                $retval[$element] = $c->values[$fieldName];
            }
        }
        return $retval;
    }

Usage Example

Пример #1
0
 public function testSelectFieldContentFieldArrayDerpy()
 {
     $app = $this->getApp();
     $handler = new RecordHandler($app);
     $record1 = $app['storage']->getEmptyContent('pages');
     $record2 = $app['storage']->getEmptyContent('pages');
     $record1->setValues(['title' => 'Bruce', 'slug' => 'clippy', 'status' => 'published']);
     $record2->setValues(['title' => 'Johno', 'slug' => 'koala', 'status' => 'published']);
     $result = $handler->selectField([$record1, $record2], ['title', 'derp'], true, 'slug');
     $this->assertSame('Bruce', $result['clippy'][0]);
     $this->assertNull($result['clippy'][1]);
     $this->assertSame('Johno', $result['koala'][0]);
     $this->assertNull($result['koala'][1]);
 }