drupal 7 module .install unable to create table - drupal

i have created module but unablet to create table below is my code of mysubscribers.install
mysubscribers.install
function mysubscribers_schema(){
$schema['mysubscribers'] = array(
'description' => 'The table for storing the subscriber data.',
'fields' => array(
'id' => array(
'description' => 'The primary identifier for subscriber.',
'type' => 'serial',
'not null' => TRUE,
'unsigned' => TRUE,
),
),
'primary key' => array('id'),
);
return $schema;
}
/**
* Implements hook_install().
*/
function mysubscribers_install() {
if (db_table_exists('mysubscribers')) {
db_drop_table('mysubscribers'); // old table from 5.x-2.x
}
}

No issues here when running your code.
You don't need to remove the DB on install.
If you make changes to the schema add your changes to hook_update()
with something like db_change_field() or db_add_field().
As long as your naming convention is correct this should work.
assuming you have a mysubscribers.info,mysubscribers.module, and mysubscribers.install

Related

New fields aren't created after updating a custom module. DRUPAL 7

I have a custom module on my site. I try to install an update with a new field for my vocabulary, but the field doesn't appear.
hook_update:
function mymodule_update_7118()
{
$field_name = 'field_newfield';
if ( field_info_field( $field_name ) ) {
return;
}
$field = array(
'field_name' => $field_name,
'type' => 'list_integer',
'settings' => array(
'allowed_values' => array(
'Yes' => 1, //heard that adding a NO value may cause problems, although it doesn't work with a no value either.
),
),
);
$field = field_create_field( $field );
$instance = array(
'field_name' => $field['field_name'],
'entity_type' => 'taxonomy',
'bundle' => 'vocab_name',
'label' => 'Label',
'widget' => array(
'active' => 1,
'module' => 'options',
'settings' => array(),
'type' => 'options_select',
'weight' => '3',
),
);
field_create_instance($instance);
}
Logs contain several recordings of Internalization module creating a string to translate this field. Also all needed tables are created in the database, but they are all empty.
For creating a new custom field you must do it like a custom module. The steps can be found out at https://drupal.stackexchange.com/questions/140517/how-to-create-new-field-type
You can find the excellent field_example module from the Examples Module which is always the first place to look. Examples module can be downloaded from https://www.drupal.org/project/examples

attach image_field to entity in drupal 7

I use drupal 7, and Entity API to develope a module. I have an entity to record client information. I wish to use image_field to let client upload their logo. So I have this function:
function silver_client_enable()
{
field_cache_clear();
field_associate_fields("silver_client");
if(field_info_field('logo'))
return;
$field = array(
'field_name' => 'logo',
'cadinality' => 1,
'type' => 'image',
);
field_create_field($field);
$instance = array(
'field_name' => 'logo',
'entity_type' => 'silver_client',
'bundle' => 'silver_client',
'label' => 'Logo',
'description' => 'Logo',
'display' => array(
'default' => array('label' => 'hidden')
),
'settings' => array(
'file_directory' => '/logo',
),
'widget' => array(
'type' => 'image_image',
),
);
field_create_instance($instance);
}
In the entity creation/edit form, I use :
field_attach_form('silver_client', $client, $form, $form_state);
to attch the field.
When I called up this form, the image upload field was corrected displayed. An i can use it to uplod file to serve.
In the form submit function, I save the entity as:
entity_save('silver_client', $client);
However, after I press the save button, the entity table is correctly saved. Field table is not. Both field_data_logo and field_revision_logo are empty.
I believer Entity API looks after the retrieving and saving of attached fields. Can someone tell me what is wrong with my code? Thank you.
You have to write the values back into your entity:
field_attach_submit('silver_client', $client, $form, $form_state);
entity_save('silver_client', $client);
http://api.drupal.org/api/drupal/modules!field!field.attach.inc/function/field_attach_submit/7
And you should validate the field values:
field_attach_validate('silver_client', $client, $form, $form_state);
http://api.drupal.org/api/drupal/modules!field!field.attach.inc/function/field_attach_validate/7
Furthermore if you don't want to declare your entity and fields by yourself you might checkout the EntityConstructionKit : http://drupal.org/project/eck which allows to export entity structures with Features just like Views.

Magento - How to add Ship to information to order grid in Magento 1.5.1.0

I found a great way to put the customer email address on the Magento Admin Sales Order grid via this Q&A (http://stackoverflow.com/questions/6416864/how-to-add-customer-email-to-order-grid-in-magento-1-4/6906254#6906254) by Ben Incani and it works great.
My question is: Using this method, how can I add the Ship to information (name, address, city, state, zip)?
I've tried doing two versions (that sort of work) but do not work fully so I am a bit stuck...
This is the code that works for customer emails:
$collection->getSelect()->joinLeft(array('sfo'=>'sales_flat_order'),'sfo.entity_id=main_table.entity_id',array('sfo.customer_email', 'sfo.shipping_description'));
Now, when trying to go into the database table that has the Ship to information I tried this:
$collection->getSelect()->joinLeft(array('sfoa'=>'sales_flat_order_address'),'sfoa.parent_id=sfo.entity_id',array('sfoa.postcode'));
This returns an error log with the message:
a:5:{i:0;s:68:"Item (Mage_Sales_Model_Order) with the same id "10860" already exist";i:1;s:5104:"#0
Trying this code (which most closely follows the original customer email code):
$collection->getSelect()->joinLeft(array('sfoa'=>'sales_flat_order_address'),'sfoa.entity_id=main_table.entity_id',array('sfoa.postcode'));
yields a grid that I can view with a populated column. However, the values in the column are NOT the correct Postal Codes - I can't even figure out what values it is pulling???
I guess one of my issues is that I don't exactly know what main_table.entity_id refers to (although I have a guess).
Anyway, I feel that I'm close and if someone can answer how I successfully get the information with this method, I'd be eternally grateful! Can anyone
Revised Answer (Due to two problematic errors)
I'm rewriting this answer in a more friendly, step by step way to hopefully help someone else out.
Note: This is for a local change at app/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php
A. In order to get everything working correclty, you first need to change the _getCollectionClass() from this:
protected function _getCollectionClass()
{
return 'sales/order_grid_collection';
}
to this:
protected function _getCollectionClass()
{
//return 'sales/order_grid_collection';
return 'sales/order_collection';
}
I ran into a major headache when doing this which was this:
SQLSTATE[23000]: Integrity constraint violation: 1052 Column ‘created_at’ in where clause is ambiguous
This happens when you try to filter/search the grid by the Purchase On column.
In order to avoid/fix this error, you need to change the collectioin AND add the following to _prepareCollection() AND add a filter_index to each of the columns added to the grid.
You'll also run into another headache
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'billing_name' in 'where clause'
IF in the _prepareCollection() you try to dynamically create the columns of the Billing Name or Shipping Name which looks like this:
$collection->getSelect()->columns(new Zend_Db_Expr("CONCAT(s2.firstname, ' ',s2.lastname) AS billing_name"));
$collection->getSelect()->columns(new Zend_Db_Expr("CONCAT(s1.firstname, ' ',s1.lastname) AS shipping_name"));
When this is done, there is no real easy way (that I came across to fix this), if it all.
In order to avoid these headaches (after you change the _getCollectionClass() to the above) do the following:
B. Change the _prepareCollection() to this:
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()->joinLeft(array('sfog' => 'sales_flat_order_grid'),'main_table.entity_id = sfog.entity_id',array('sfog.shipping_name','sfog.billing_name'));
$collection->getSelect()->joinLeft(array('sfo'=>'sales_flat_order'),'sfo.entity_id=main_table.entity_id',array('sfo.customer_email','sfo.weight','sfo.discount_description','sfo.increment_id','sfo.store_id','sfo.created_at','sfo.status','sfo.base_grand_total','sfo.grand_total'));
$collection->getSelect()->joinLeft(array('sfoa'=>'sales_flat_order_address'),'main_table.entity_id = sfoa.parent_id AND sfoa.address_type="shipping"',array('sfoa.street','sfoa.city','sfoa.region','sfoa.postcode','sfoa.telephone'));
$this->setCollection($collection);
return parent::_prepareCollection();
}
C. Then for the existing columns in _prepareColumns() add a filer_index to each:
Example:
$this->addColumn('billing_name', array(
'header' => Mage::helper('sales')->__('Bill to Name'),
'index' => 'billing_name',
'filter_index' => 'sfog.billing_name',
));
D. Then add the columns you want to add like this:
Example:
$this->addColumn('customer_email', array(
'header' => Mage::helper('sales')->__('Customer Email'),
'index' => 'customer_email',
'filter_index' => 'sfo.customer_email',
'width' => '50px',
));
I have a similar requirement some time back, where I need to add customer email and shipping region to the sales order grid.
For achieving the requirement I have rewritten class Mage_Adminhtml_Block_Sales_Order_Grid as below in my custom module.
class Custom_OrderGrid_Block_Adminhtml_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid {
protected function _getCollectionClass() {
return 'sales/order_collection';
}
protected function _prepareCollection() {
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()->joinLeft(array('sfog' => 'sales_flat_order_grid'), 'main_table.entity_id = sfog.entity_id', array('sfog.shipping_name', 'sfog.billing_name'));
$collection->getSelect()->joinLeft(array('sfo' => 'sales_flat_order'), 'sfo.entity_id=main_table.entity_id', array('sfo.customer_email', 'sfo.increment_id', 'sfo.store_id', 'sfo.created_at', 'sfo.status', 'sfo.base_grand_total', 'sfo.grand_total'));
$collection->getSelect()->joinLeft(array('sfoa' => 'sales_flat_order_address'), 'main_table.entity_id = sfoa.parent_id AND sfoa.address_type="shipping"', array('sfoa.region'));
$this->setCollection($collection);
return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
}
protected function _prepareColumns() {
$this->addColumn('real_order_id', array(
'header' => Mage::helper('sales')->__('Order #'),
'width' => '80px',
'type' => 'text',
'index' => 'increment_id',
'filter_index' => 'sfo.increment_id'
));
if (!Mage::app()->isSingleStoreMode()) {
$this->addColumn('store_id', array(
'header' => Mage::helper('sales')->__('Purchased From (Store)'),
'index' => 'store_id',
'type' => 'store',
'store_view' => true,
'display_deleted' => true,
'filter_index' => 'sfo.store_id'
));
}
$this->addColumn('created_at', array(
'header' => Mage::helper('sales')->__('Purchased On'),
'index' => 'created_at',
'type' => 'datetime',
'width' => '100px',
'filter_index' => 'sfo.created_at'
));
$this->addColumn('billing_name', array(
'header' => Mage::helper('sales')->__('Bill to Name'),
'index' => 'billing_name',
'filter_index' => 'sfog.billing_name'
));
$this->addColumn('shipping_name', array(
'header' => Mage::helper('sales')->__('Ship to Name'),
'index' => 'shipping_name',
'filter_index' => 'sfog.shipping_name'
));
$this->addColumn('customer_email', array(
'header' => Mage::helper('sales')->__('Customer Email'),
'index' => 'customer_email',
'filter_index' => 'sfo.customer_email',
'width' => '50px',
));
$this->addColumn('region', array(
'header' => Mage::helper('sales')->__('Shipping State'),
'index' => 'region',
'filter_index' => 'sfoa.region',
'width' => '50px',
));
$this->addColumn('base_grand_total', array(
'header' => Mage::helper('sales')->__('G.T. (Base)'),
'index' => 'base_grand_total',
'type' => 'currency',
'currency' => 'base_currency_code',
'filter_index' => 'sfo.base_grand_total'
));
$this->addColumn('grand_total', array(
'header' => Mage::helper('sales')->__('G.T. (Purchased)'),
'index' => 'grand_total',
'type' => 'currency',
'currency' => 'order_currency_code',
'filter_index' => 'sfo.grand_total'
));
$this->addColumn('status', array(
'header' => Mage::helper('sales')->__('Status'),
'index' => 'status',
'type' => 'options',
'width' => '70px',
'filter_index' => 'sfo.status',
'options' => Mage::getSingleton('sales/order_config')->getStatuses(),
));
if (Mage::getSingleton('admin/session')->isAllowed('sales/order/actions/view')) {
$this->addColumn('action', array(
'header' => Mage::helper('sales')->__('Action'),
'width' => '50px',
'type' => 'action',
'getter' => 'getId',
'actions' => array(
array(
'caption' => Mage::helper('sales')->__('View'),
'url' => array('base' => '*/sales_order/view'),
'field' => 'order_id'
)
),
'filter' => false,
'sortable' => false,
'index' => 'stores',
'is_system' => true,
));
}
$this->addRssList('rss/order/new', Mage::helper('sales')->__('New Order RSS'));
$this->addExportType('*/*/exportCsv', Mage::helper('sales')->__('CSV'));
$this->addExportType('*/*/exportExcel', Mage::helper('sales')->__('Excel XML'));
return Mage_Adminhtml_Block_Widget_Grid::_prepareColumns();
}
}
I hope this will help others.
The reason about that error "Mage_Sales_Model_Order) with the same id "10860" already exist" it's because sales_flat_order_address creates two records for every order, one for shipping address and another one for billing address, if you add this filter on the _prepareCollection() you can make it work
$collection->getSelect()->where("address_type='shipping'");

Correct way to use Drupal 7 Entities and Field API

I'm trying to use Drupal 7's entities and field API to correctly build a new module. What I have been unable to understand from the documentation is the correct way to use the new API to create a 'content type' (not a node type) with a number of set fields, such as Body.
I'm trying to set up the entity using hook_entity_info, then I believe I need to add the body field using field_create_instance, but I can't seem to get it to work.
In mycontenttype.module:
/**
* Implements hook_entity_info().
*/
function mycontenttype_entity_info() {
$return = array(
'mycontenttype' => array(
'label' => t('My Content Type'),
'controller class' => 'MyContentTypeEntityController',
'base table' => 'content_type',
'uri callback' => 'content_type_uri',
'entity keys' => array(
'id' => 'cid',
'label' => 'title',
),
'bundles' => array(
'mycontenttype' => array(
'label' => 'My Content Type',
'admin' => array(
'path' => 'admin/contenttype',
'access arguments' => array('administer contenttype'),
),
),
),
'fieldable' => true,
),
);
return $return;
}
/**
* Implements hook_field_extra_fields().
*/
function mycontenttype_field_extra_fields() {
$return['mycontenttype']['mycontenttype'] = array(
'form' => array(
'body' => array(
'label' => 'Body',
'description' => t('Body content'),
'weight' => 0,
),
),
);
return $return;
}
Then does this go in the .install file?
function mycontenttype_install() {
$field = array(
'field_name' => 'body',
'type' => 'text_with_summary',
'entity_types' => array('survey'),
'translatable' => TRUE,
);
field_create_field($field);
$instance = array(
'entity_type' => 'mycontenttype',
'field_name' => 'body',
'bundle' => 'mycontenttype',
'label' => 'Body',
'widget_type' => 'text_textarea_with_summary',
'settings' => array('display_summary' => TRUE),
'display' => array(
'default' => array(
'label' => 'hidden',
'type' => 'text_default',
),
'teaser' => array(
'label' => 'hidden',
'type' => 'text_summary_or_trimmed',
),
),
);
field_create_instance($instance);
}
I think your problem is that if node module is installed, there is already a field named 'body'. You should either re-name your field to something like 'mycontenttype_body' (comment.module uses comment_body), or re-use the 'body' field and skip the adding the field part and skip to adding the instance of it. The former is recommended over the latter.
Every field has an array property, entity_types, which limits the entities to which the field can be attached.
The best Drupal solution I can find, hook_field_create_field, can alter fields as they are created, but that's no good for the body field which is created on installation.
So my solution is just to edit the database directly in my hook_install
$data_col = db_query("SELECT data from field_config where field_name = 'body'")->fetchAssoc();
$data = unserialize($data_col['data']);
$data['entity_types'][] = 'MY_ENTITY_TYPE';
db_update('field_config')
->fields(array('data' => array('data' => serialize($data))))
->condition('field_name', 'body')
->execute();
just started down the same path here is a video from fago
Here's a nice repo to start: Lawmakers entity

Drupal 6 module install file not creating tables in database

I'm using the Schema API to create tables for my module on Drupa 6.17, but the tables just do not get created in the database. I have the Schema module installed, and it tells me that while the schema for my module is recognized, its table is not in the database. It comes up under Missing:
Tables in the schema that are not present in the database.
test
* test_table
Here are the contents for my test.install file.
<?php
// $Id$
function test_schema() {
$schema['test_table'] = array(
'description' => t('Test table'),
'fields' => array(
'nid' => array(
'description' => t('test field'),
'type' => 'serial',
'not null' => TRUE,
),
'options' => array(
'description' => t('other test field'),
'type' => 'text',
'not null' => FALSE,
),
),
'primary key' => array('nid'),
);
return $schema;
}
function test_install() {
drupal_install_schema('test');
}
function test_uninstall() {
drupal_uninstall_schema('test');
}
if you want to add module install after the module creation you need to remove the record from system table in your drupal db, and then enable it again.
disable your module and save
goto 'system' table and find your module there
remove that record
enable your module and save
Edit:
Here is code I just wrote that works. Follow as example:
/**
* Implementation of hook_schema().
*/
function action_alert_schema() {
$schema['action_alert'] = array(
'description' => 'Action Alert table.',
'fields' => array(
'aid' => array(
'description' => 'The serial ID.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'nid' => array(
'description' => 'The primary identifier of the node.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'uuid' => array(
'description' => 'The session id of the user if the UID is not present.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '0',
),
),
'primary key' => array('aid'),
);
return $schema;
}
/**
* Implementation of hook_install().
*/
function action_alert_install() {
drupal_install_schema('action_alert');
}
/**
* Implementation of hook_uninstall().
*/
function action_alert_uninstall() {
drupal_uninstall_schema('action_alert');
}
Drupal only runs a module's hook_install() once when you first enable the module. Unless you go through and disable, uninstall, and then re-enable the module will your module's hook_install() get called ever again.
If you had already created a release of your module and are wanting to add a schema to existing installs, you will want to add an implementation of hook_update_N() that calls db_create_table().
I would like to share with you my experiences with this error on Drupal 6. In my first-ever module I had three tables. I had an entry for each in my hook_schema (called education_schema):
function education_schema()
{
$schema['education_course'] = array( /* ... */ );
$schema['education_market'] = array( /* ... */ );
$schema['education_event'] = array( /* ... */ );
}
In my hook_install, I initially had the following:
function education_install()
{
drupal_install_schema('education_course');
drupal_install_schema('education_market');
drupal_install_schema('education_event');
}
No tables were creating at module install. Why? I had no idea: no errors to be seen anywhere in the logs. Eventually I found out about the PHP extension xdebug, which, when used in education_install revealed that drupal_install_schema failed because it could not find the routines education_course_schema, education_course_market and education_course_event. At that point the solution was quite obvious:
function education_install()
{
drupal_install_schema('education');
}
And voila, it worked!
So, I learned that drupal_install_schema does not log any error when it fails, that only one call to drupal_install_schema is required and that it installs all schemas that you return in the array, that the API documentation of drupal_install_schema is worth reading, and finally that xdebug is a very handy utility!

Resources