Sunday, February 9, 2014

Drupal 7: Changing Field Types for a Content Type When Data Exists

Sometimes you may need to change the field type for a content type when there is already data for that content type. But through Drupal's interface it won't let you do it because data already exists for a particular content type. I figured out that you can do this by changing some things in the tables using phpMyAdmin.

I will not go into detail on what phpMyAdmin is or how to access it on your server. So this may be advanced for you. You should probably read up on what phpMyAdmin is and how to use it. Knowing some basic SQL and understanding on database structure will get you started.

For those that are comfortable using phpMyAdmin, login to it and locate the table called field_config in your drupal schema. When you find field_conifig, click on it, then click the browse tab.

Look in the "field_name" column for the name of the field that you want to change the datatype for.

Once you find your field, click the edit icon, it looks like a pencil.

In the "field" column your will see a row called "type". This is where you need to make the change. The last column called "value" needs to be changed to the new type you want.

Some possible values are list_text, datetime, text, number_float, list_boolean, image, text_long. There are so others and may depend on what modules you have installed.

Note, you may need to change the "module" column as well. Again, you need to have some understand of what data types are. So possible values are list, text, number, date, image, etc...

Set it to your new field type and click the "Go" button.

If you are converting from character or varchar to a number or int you will need to make another change in another table. If you do, locate a table called field_data_MY_FIELD_NAME table where MY_FIELD_NAME is the drupal name of the field you are changing. Here you can change the field_MY_FIELD_NAME_value type to the data type you need.

Once you do all that. Go to your content type in Drupal admin and make any other changes that may need to be done. Some times after making changes to the database you will need to change the widget type.






Friday, November 15, 2013

Drupal 7 - Fatal error: Allowed memory size of X bytes exhausted

I encountered "Fatal error: Allowed memory size of 64233433 exhausted when I tried to copy an existing Drupal 7 installation to another host. I also go this error when I installed ctools module. After doing the following the problem went away.

Try adding one of the following lines to sites/default/settings.php file

ini_set('memory_limit', '32M');
ini_set('memory_limit', '64M');
ini_set('memory_limit', '128M');
ini_set('memory_limit', '256M');
ini_set('memory_limit', '512M');

Adding the 128M memory limit setting worked for me on shared hosting.

During my research I found other solutions that include adding memory configuration to .htaccess and php.ini files. Adding the suggested memory configurations to those files did nothing. My problem was not solved until I added ini_set('memory_limit', '128M'); to sites/default/settings.php file.

How to install Drupal on 000webhost

How to install Drupal 7 on 000webhost


1. Login to your cpanel and create your database using Mysql and write down your database information.

EXAMPLE:
-------------------------------------------------
$mysql_host = "XXXXX.000webhost.com";
$mysql_database = "a######_DATABASE_NAME";
$mysql_user = "a######_USERNAME";
$mysql_password = "PASSWORD";

NOTE: Later, when you install and are at the database screen, make sure to click advance to enter XXXXX.000webhost.com to database host field

2.  In filemanager, use "archives (zip, tar, tgz, gz)" to upload your Drupal distribution file.

3. In  your browser, got to view your site to begin Drupal 7 installation and configuration.

Wednesday, October 2, 2013

How To Move A Drupal 7 Website to Another Diretory

Move all the files.
Edit .htaccess comment out #RewriteBase /drupal/
don't need to edit $base_url in sites/default/settings.php  unless it says something else. just comment it out

Drupal Module: browscap - Current browscap data version: Never fetched



I installed a Drupal module called browscap to detect mobile devices. Both the dev version and the stable version did not work on my Drupal 7 installation. Browscap provides an improved version of PHP's get_browser() function. Browscap stores browser data in a database and automatically retrieving the latest data on a configurable schedule. It uses this data to help detect mobile devices such as smart phones and tablets.

When I went to administration | system | browscap, I saw the following problem.

After searching for a while found a solution that fixed my problem.

Current browscap data version: Never fetched

You need to SSH into your server and modify the import.inc file. This is usually located at sites/all/modules/browscap. Locate the file and open it with a text editor such as vi or emacs. Locate the following line. Mine was around line 50.

$browscap_data = drupal_http_request('http://tempdownloads.browserscap.com/stream.php?BrowsCapINI');

And change it too the following line.

$browscap_data = drupal_http_request('http://tempdownloads.browserscap.com/stream.php?PHP_BrowsCapINI');

I uninstalled the module by first disabling it, then uninstall using the Drupal admin interface. After that I enabled it and the "Never fetched" message turned into a version number. There on out the browscap module began working and correctly detected mobile devices.

Thursday, May 9, 2013

Copy WordPress Website to Local PC


At one point you may want to have a copy of your WordPress website on your local machine. There are many reasons you may want to do this. You may want to test WordPress themes, plugins, or make changes to  your WordPress blog without the risk of messing up your production website. If you have a web server running on your local PC, like Apache or IIS, PHP installed and running, and MySQL, you can easily download your WordPress blog and run it on your localhost.

Follow these steps to get your WordPress site on your local machine.

Step 1:
Copy all files from your webhost to your local server folder. In most cases this will include the following files and directories.

directories
wp-admin
wp-content
wp-includes

files
all php files that begin with "wp-"
xmlrpc.php (if you have it)
index.php
webformmailer.php (if you have it)


Step 2:
On your web host, login to phpmyadmin and export your database (By Selecting your database and selecting the export tab and downloading to your computer). Be sure to check the option to create database.


Step 3:
Log into your local phpmyadmin or MySQL Workbench, and import the sql database.


Step 4:
Once the WordPress database is created in your local MySQL, you will want to modify the database to reflect the new blog location being on the localhost. So you will need to run the following SQL on your local MySQL.

Change "YOUR_DOMAIN" with your domain name

UPDATE wp_options SET option_value = replace(option_value, 'http://www.YOUR_DOMAIN.com', 'http://localhost') WHERE option_name = 'home' OR option_name = 'siteurl';



Step 5:
Now you want to fix the urls of posts and pages. So you need to run the following SQL on your local MySQL database:


Change "YOUR_DOMAIN" with your domain name

UPDATE wp_posts SET guid = replace(guid, 'http://www.YOUR_DOMAIN.com','http://localhost');



Step 6:
If you have linked internally within blog posts or pages with absolute URLs, these links will point to wrong locations after you move the blog location. So we run the following SQL to fix this issue:



Change "YOUR_DOMAIN" with your domain name

UPDATE wp_posts SET post_content = replace(post_content, 'http://www.YOUR_DOMAIN.com', 'http://localhost');



Step 7:
You now want to now modify you wp-config.php file on your local host. The easiest way to do this is to delete the wp-config.php file on your local host and then visit http://localhost in a browser.

Step 8:
Create a MySQL user for your WordPress database. For simplicity, use the same username that is on your hosted WordPress site. Grant it all rights, and assign it to the new WordPress schema(database).

Step 9:
With the dialog message that comes up click the 'Create a Configuration File' button. Then click the 'Lets Go' button on the next screen.


Step 10:
Fill in all of the fields:
Database Name: This is the name of the database in your local phpmyadmin
Username: This is your phpmyadmin username (usually root)
Password: This is your phpmyadmin username's password
Database Host: This is usually localhost
Database Prefix: normally is wp_ unless someone has changed it manually.


Step 11:
Click Submit, the wp-config file will be created and everything should work on the local host. Do not go through the WordPress set up.

Note: If you WordPress theme has a skin, you may need to reactivate it in order to get the skin working again.




Sunday, April 14, 2013

Handling Invalid HTML with Python Mechanize

Python's Mechanize not recognizing a form or HTML? Try BeautifulSoup


If you are using the python mechanize module to parse HTML forms and run into problems, you can try using BeautifulSoup's prettify function to fix the HTML that mechanize retrieves.

If you don't already have it installed, download and install BeautifulSoup 3.2.1 from http://www.crummy.com/software/BeautifulSoup/

Don't use BeautifulSoup4. It is messed up and I couldn't get it working. At the time I tried to use it, it didn't work and searches on the internet showed that it hasn't been working for a while for most people.

If you have easy_install you can just type "easy_install -Z beautifulsoup". After it installs, you might have to go to site-packages and move the BeautifulSoup.py file from it's own directory into site-packages itself. It wouldn't import for me until I moved it.

Import mechanize and BeautifulSoup
import mechanize
from BeautifulSoup import BeautifulSoup

Put the following class into your python script
class PrettifyHandler(mechanize.BaseHandler):
    def http_response(self, request, response):
        if not hasattr(response, "seek"):
            response = mechanize.response_seek_wrapper(response)
        # only use BeautifulSoup if response is html
        if response.info().dict.has_key('content-type') and ('html' in response.info().dict['content-type']):
            soup = BeautifulSoup(response.get_data())
            response.set_data(soup.prettify())
        return response

When you create a browser with mechanize, add the following handler.
br = mechanize.Browser()
br.add_handler(PrettifyHandler())

Now just use mechanize like normal. The mechanize browser will now use BeautifulSoup to parse all responses where html is contained in the contect type (mime type) text/html. This helped me get mechanize to work with forms