Sonata\CustomerBundle\Twig\Extension\AddressExtension::renderAddress PHP Method

renderAddress() public method

Gets the HTML of an address.
public renderAddress ( Twig_Environment $environment, mixed $address, boolean $showName = true, boolean $showEdit = false, string $context = null ) : string
$environment Twig_Environment A Twig environment
$address mixed An instance of AddressInterface or array with keys: (id, firstname, lastname, address1, postcode, city, country_code and optionally name, address2, address3)
$showName boolean Display address name?
$showEdit boolean Display edit button?
$context string A context for edit link
return string
    public function renderAddress(\Twig_Environment $environment, $address, $showName = true, $showEdit = false, $context = null)
    {
        $requiredAddressKeys = array('firstname', 'lastname', 'address1', 'postcode', 'city', 'country_code');
        if (!$address instanceof AddressInterface && (!is_array($address) || count(array_diff($requiredAddressKeys, array_keys($address))) !== 0)) {
            throw new InvalidParameterException(sprintf('sonata_address_render needs an AddressInterface instance or an array with keys (%s)', implode(', ', $requiredAddressKeys)));
        }
        if ($address instanceof AddressInterface) {
            $addressArray = array('id' => $showEdit ? $address->getId() : '', 'name' => $showName ? $address->getName() : '', 'address' => $address->getFullAddressHtml());
        } else {
            if ($showEdit && !array_key_exists('id', $address)) {
                throw new InvalidParameterException("sonata_address_render needs 'id' key to be set to render the edit button");
            }
            if ($showName && !array_key_exists('name', $address)) {
                $address['name'] = '';
                $showName = false;
            }
            $addressArray = array('id' => $showEdit ? $address['id'] : '', 'name' => $address['name'], 'address' => BaseAddress::formatAddress($address, '<br/>'));
        }
        return $environment->render('SonataCustomerBundle:Addresses:_address.html.twig', array('address' => $addressArray, 'showName' => $showName, 'showEdit' => $showEdit, 'context' => $context));
    }

Usage Example

 /**
  * @expectedException Sonata\CoreBundle\Exception\InvalidParameterException
  * @expectedExceptionMessage sonata_address_render needs 'id' key to be set to render the edit button
  */
 public function testRenderAddressMissingId()
 {
     $environment = $this->getMockBuilder('Twig_Environment')->disableOriginalConstructor()->getMock();
     $address = array("firstname" => "", "lastname" => "", "address1" => "", "postcode" => "", "city" => "", "country_code" => "");
     $extension = new AddressExtension();
     $extension->renderAddress($environment, $address, true, true);
 }
All Usage Examples Of Sonata\CustomerBundle\Twig\Extension\AddressExtension::renderAddress