Skip to content

Commit

Permalink
added option to override default years in year widget in Field model/…
Browse files Browse the repository at this point in the history
…interface
  • Loading branch information
teo-sk committed Oct 15, 2012
1 parent c595553 commit 23f82ba
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
23 changes: 16 additions & 7 deletions Form/PollType.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,25 @@ public function buildForm(FormBuilder $builder, array $options)
$options
);
} else {
$additionalOptions = array();
$options = array(
'label' => array(
'label' => $field->getTitle(),
'description' => $field->getDescription()
),
'required' => $field->isRequired()
);

if ($field->getType() == FieldInterface::TYPE_DATE
|| $field->getType() == FieldInterface::TYPE_DATETIME) {
$additionalOptions['years'] = $field->getYearRange();
}

$finalOptions = array_merge($options, $additionalOptions);
$builder->add(
sprintf('field_%s', $field->getId()),
$this->fieldTypes[$field->getType()],
array(
'label' => array(
'label' => $field->getTitle(),
'description' => $field->getDescription()
),
'required' => $field->isRequired(),
)
$finalOptions
);
}
}
Expand Down
14 changes: 14 additions & 0 deletions Model/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,4 +391,18 @@ public function isRenderable()
{
return $this->isActive() && $this->getDeletedAt() === null;
}

/**
* {@inheritDoc}
*/
public function getYearRange()
{
$begin = strtotime("-5 years", time());
$end = strtotime("+5 years", time());

return range(date("Y", $begin), date("Y", $end));

}


}
7 changes: 7 additions & 0 deletions Model/FieldInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,11 @@ public function isActive();
* @return bool
*/
public function isRenderable();

/**
* Returns array of years to be used by default in year widgets
*
* @return array
*/
public function getYearRange();
}

0 comments on commit 23f82ba

Please sign in to comment.