Skip to content

Instantly share code, notes, and snippets.

@ianshea
Last active August 29, 2015 14:04
Show Gist options
  • Save ianshea/a86cf3dd9c8a72c2244f to your computer and use it in GitHub Desktop.
Save ianshea/a86cf3dd9c8a72c2244f to your computer and use it in GitHub Desktop.
CraftCMS - Custom Fieldtype w/ Model
{% import "_includes/forms" as forms %}
{{ forms.hidden({
id: model.handle ~ 'handle',
name: model.handle ~ '[handle]',
value: model.handle is defined ? model.handle
})
}}
<div id="{{ model.handle }}" class="blockbutton">
<table>
<tr>
<td>Label:</td>
<td>Color:</td>
<td>Size:</td>
</tr>
<tr>
<td>
{{ forms.textField({
id: model.handle ~ 'label',
name: model.handle ~ '[label]',
value: model.label is defined ? model.label
})
}}
</td>
<td>
{{ forms.selectField({
id: model.handle ~ 'color',
name: model.handle ~ '[color]',
options: settings.color_options,
value: model.color is defined ? model.color
}) }}
</td>
<td>
{{ forms.selectField({
id: model.handle ~ 'size',
name: model.handle ~ '[size]',
options: settings.size_options,
value: model.size is defined ? model.size
}) }}
</td>
</tr>
</table>
</div>
<?php
namespace Craft;
class BlockButton_FieldType extends BaseFieldType
{
public function getName()
{
return Craft::t('Block Button');
}
public function defineContentAttribute()
{
return Array( AttributeType::Mixed, 'model' => 'BlockButtonModel' );
}
public function getInputHtml($name, $value)
{
$size_options = array( 'default-size' => 'Default', 'btn-md' => 'Medium', 'btn-sm' => 'Small' );
$color_options = array(
'dk-blue' => 'Default',
'orange' => 'Orange',
'lt-blue' => 'Light Blue',
'lime' => 'Lime',
'teal' => 'Teal',
'salmon' => 'Salmon',
'red' => 'Red',
'green' => 'Green',
'fuschia' => 'Fuschia',
'purple' => 'Purple',
'gray' => 'Gray',
);
$size_options = array(
'' => 'Default',
'btn-md' => 'Medium',
'btn-sm' => 'Small',
);
if (!empty($value))
{
$buttonModel = BlockButtonModel::populateModel($value);
} else {
$buttonModel = new BlockButtonModel;
$buttonModel->handle = $name;
}
return craft()->templates->render('blockbutton/_fieldtypes/blockbutton', array(
'model' => $buttonModel->getAttributes(),
'settings' => array(
'color_options' => $color_options,
'size_options' => $size_options )
));
}
}
<?php
namespace Craft;
class BlockButtonModel extends BaseModel
{
protected function defineAttributes()
{
return array(
'handle' => AttributeType::String,
'label' => AttributeType::String,
'color' => array( AttributeType::String, 'default' => 'dk-blue' ),
'size' => array( AttributeType::String, 'default' => '' ),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment